Python Pandas – Can we use & Operator to find common columns between two DataFrames?

Yes, we can use the & operator to find the common columns between two DataFrames. The & operator performs a set intersection operation on DataFrame column indexes, returning only the columns that exist in both DataFrames.

Creating Two DataFrames

Let's create two DataFrames with some overlapping columns ?

import pandas as pd

# Creating dataframe1
dataFrame1 = pd.DataFrame({
    "Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'],
    "Cubic_Capacity": [2000, 1800, 1500, 2500, 2200, 3000],
})

print("Dataframe1...\n", dataFrame1)

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

print("Dataframe2...\n", dataFrame2)
Dataframe1...
       Car  Cubic_Capacity
0      BMW            2000
1    Lexus            1800
2    Tesla            1500
3  Mustang            2500
4 Mercedes            2200
5   Jaguar            3000
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 Using & Operator

The & operator performs a set intersection on the column indexes ?

import pandas as pd

# Creating dataframe1
dataFrame1 = pd.DataFrame({
    "Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'],
    "Cubic_Capacity": [2000, 1800, 1500, 2500, 2200, 3000],
})

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

# Getting common columns using the & operator
common_columns = dataFrame1.columns & dataFrame2.columns

print("Common columns...\n", common_columns)
Common columns...
Index(['Car'], dtype='object')

Alternative Methods

You can also use intersection() method for the same result ?

import pandas as pd

dataFrame1 = pd.DataFrame({
    "Car": ['BMW', 'Lexus'],
    "Price": [50000, 45000],
    "Model": ['X5', 'ES']
})

dataFrame2 = pd.DataFrame({
    "Car": ['BMW', 'Lexus'],
    "Price": [52000, 47000],
    "Year": [2023, 2022]
})

# Using intersection method
common_cols = dataFrame1.columns.intersection(dataFrame2.columns)
print("Common columns using intersection():", common_cols)

# Using & operator
common_cols_ampersand = dataFrame1.columns & dataFrame2.columns
print("Common columns using &:", common_cols_ampersand)
Common columns using intersection(): Index(['Car', 'Price'], dtype='object')
Common columns using &: Index(['Car', 'Price'], dtype='object')

Conclusion

The & operator is an efficient way to find common columns between DataFrames by performing set intersection on their column indexes. Both & operator and intersection() method produce the same result.

Updated on: 2026-03-26T13:16:33+05:30

327 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements