Python - Fetch columns between two Pandas DataFrames by Intersection

To fetch columns between two DataFrames by intersection, use the intersection() method. This method returns the common column names present in both DataFrames.

Syntax

dataframe.columns.intersection(other_dataframe.columns)

Creating Sample DataFrames

Let's create two DataFrames with some common and different columns ?

import pandas as pd

# Creating dataframe1
dataFrame1 = pd.DataFrame({
    "Car": ['Bentley', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'],
    "Cubic_Capacity": [2000, 1800, 1500, 2500, 2200, 3000],
    "Reg_Price": [7000, 1500, 5000, 8000, 9000, 6000]
})

print("Dataframe1...")
print(dataFrame1)
Dataframe1...
       Car  Cubic_Capacity  Reg_Price
0  Bentley            2000       7000
1    Lexus            1800       1500
2    Tesla            1500       5000
3  Mustang            2500       8000
4 Mercedes            2200       9000
5   Jaguar            3000       6000
import pandas as pd

# Creating dataframe2
dataFrame2 = pd.DataFrame({
    "Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'],
    "Units_Sold": [100, 110, 150, 80, 200, 90]
})

print("Dataframe2...")
print(dataFrame2)
Dataframe2...
       Car  Units_Sold
0      BMW         100
1    Lexus         110
2    Tesla         150
3  Mustang          80
4 Mercedes         200
5   Jaguar          90

Finding Common Columns

Use the intersection() method to find columns that exist in both DataFrames ?

import pandas as pd

# Creating dataframes
dataFrame1 = pd.DataFrame({
    "Car": ['Bentley', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'],
    "Cubic_Capacity": [2000, 1800, 1500, 2500, 2200, 3000],
    "Reg_Price": [7000, 1500, 5000, 8000, 9000, 6000]
})

dataFrame2 = pd.DataFrame({
    "Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'],
    "Units_Sold": [100, 110, 150, 80, 200, 90]
})

# Getting common columns using intersection()
common_columns = dataFrame2.columns.intersection(dataFrame1.columns)
print("Common columns:")
print(common_columns)
Common columns:
Index(['Car'], dtype='object')

Using Common Columns to Select Data

Once you have the common columns, you can use them to select specific data from both DataFrames ?

import pandas as pd

# Creating dataframes
dataFrame1 = pd.DataFrame({
    "Car": ['Bentley', 'Lexus', 'Tesla'],
    "Price": [70000, 50000, 80000],
    "Year": [2020, 2019, 2021]
})

dataFrame2 = pd.DataFrame({
    "Car": ['BMW', 'Lexus', 'Audi'],
    "Price": [60000, 55000, 65000],
    "Model": ['X5', 'RX', 'Q7']
})

# Find common columns
common_cols = dataFrame1.columns.intersection(dataFrame2.columns)
print("Common columns:", list(common_cols))

# Select only common columns from both dataframes
print("\nDataFrame1 common columns:")
print(dataFrame1[common_cols])

print("\nDataFrame2 common columns:")
print(dataFrame2[common_cols])
Common columns: ['Car', 'Price']

DataFrame1 common columns:
       Car  Price
0  Bentley  70000
1    Lexus  50000
2    Tesla  80000

DataFrame2 common columns:
     Car  Price
0    BMW  60000
1  Lexus  55000
2   Audi  65000

Conclusion

The intersection()

Updated on: 2026-03-26T13:15:40+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements