Python Pandas: How to Get the Common Index of Two Pandas DataFrames in Python
When working with multiple DataFrames in Pandas, you often need to identify which index labels they share. Finding common indices is essential for merging, filtering, comparing, or aligning data across DataFrames. In this guide, we'll show you multiple methods to find common indices and demonstrate practical use cases like filtering and merging based on shared indices.
Why Find Common Indices?
Common indices are useful when you need to:
- Filter DataFrames to only include rows present in both datasets.
- Merge DataFrames on their index, keeping only shared rows.
- Compare values across DataFrames for the same entities.
- Validate data consistency between two sources.
Setup: Creating Sample DataFrames
import pandas as pd
df1 = pd.DataFrame(
{"score": [85, 90, 78, 92]},
index=["Alice", "Bob", "Carol", "Dave"]
)
df2 = pd.DataFrame(
{"grade": ["A", "B+", "A-", "B"]},
index=["Bob", "Carol", "Dave", "Eve"]
)
print("DataFrame 1:")
print(df1)
print("\nDataFrame 2:")
print(df2)
Output:
DataFrame 1:
score
Alice 85
Bob 90
Carol 78
Dave 92
DataFrame 2:
grade
Bob A
Carol B+
Dave A-
Eve B
df1 has indices ['Alice', 'Bob', 'Carol', 'Dave'] and df2 has ['Bob', 'Carol', 'Dave', 'Eve']. The common indices are ['Bob', 'Carol', 'Dave'].
Methods to Find Common Indices
Method 1: Using .intersection() (Recommended)
The .intersection() method on Pandas Index objects returns the shared labels between two indices:
common = df1.index.intersection(df2.index)
print(common)
print(type(common))
Output:
Index(['Bob', 'Carol', 'Dave'], dtype='object')
<class 'pandas.core.indexes.base.Index'>
This returns a Pandas Index object, which can be used directly for indexing and filtering.
.intersection() is the most idiomatic and efficient approach because it stays within the Pandas ecosystem, preserves index type and ordering, and integrates seamlessly with other Pandas operations.
Method 2: Using the & Operator (ONLY before Pandas v2.x)
The & operator performs set intersection on Index objects:
common = df1.index & df2.index
print(common)
Output:
Index(['Bob', 'Carol', 'Dave'], dtype='object')
This is functionally identical to .intersection() but uses operator syntax.
Method 3: Using Python Set Operations
Convert the indices to Python sets and use the & (intersection) operator:
common_set = set(df1.index) & set(df2.index)
print(common_set)
Output:
{'Carol', 'Bob', 'Dave'}
Sets are unordered, so the result won't preserve the original index order. If order matters, convert back to a sorted list or use the Pandas .intersection() method instead:
# Preserving order
common_sorted = sorted(set(df1.index) & set(df2.index))
print(common_sorted) # ['Bob', 'Carol', 'Dave']
Method 4: Using np.intersect1d()
NumPy's intersect1d() finds common elements between two arrays and returns them sorted:
import numpy as np
import pandas as pd
df1 = pd.DataFrame(
{"score": [85, 90, 78, 92]},
index=["Alice", "Bob", "Carol", "Dave"]
)
df2 = pd.DataFrame(
{"grade": ["A", "B+", "A-", "B"]},
index=["Bob", "Carol", "Dave", "Eve"]
)
common = np.intersect1d(df1.index, df2.index)
print(common)
Output:
['Bob' 'Carol' 'Dave']
This returns a sorted NumPy array.
Method 5: Using .isin() for Boolean Masking
The .isin() method creates a boolean mask showing which indices of one DataFrame exist in another:
mask = df1.index.isin(df2.index)
print(mask)
print(df1.index[mask])
Output:
[False True True True]
Index(['Bob', 'Carol', 'Dave'], dtype='object')
This approach is especially useful when you want to directly filter a DataFrame rather than just extract the common index values.
Practical Use Cases
Filtering DataFrames by Common Index
Once you have the common indices, use .loc[] to filter both DataFrames:
import numpy as np
import pandas as pd
df1 = pd.DataFrame(
{"score": [85, 90, 78, 92]},
index=["Alice", "Bob", "Carol", "Dave"]
)
df2 = pd.DataFrame(
{"grade": ["A", "B+", "A-", "B"]},
index=["Bob", "Carol", "Dave", "Eve"]
)
common = df1.index.intersection(df2.index)
df1_filtered = df1.loc[common]
df2_filtered = df2.loc[common]
print("Filtered DataFrame 1:")
print(df1_filtered)
print("\nFiltered DataFrame 2:")
print(df2_filtered)
Output:
Filtered DataFrame 1:
score
Bob 90
Carol 78
Dave 92
Filtered DataFrame 2:
grade
Bob A
Carol B+
Dave A-
Merging DataFrames on Common Index
Use pd.merge() with an inner join to combine DataFrames based on shared indices:
import numpy as np
import pandas as pd
df1 = pd.DataFrame(
{"score": [85, 90, 78, 92]},
index=["Alice", "Bob", "Carol", "Dave"]
)
df2 = pd.DataFrame(
{"grade": ["A", "B+", "A-", "B"]},
index=["Bob", "Carol", "Dave", "Eve"]
)
merged = pd.merge(df1, df2, left_index=True, right_index=True, how='inner')
print(merged)
Output:
score grade
Bob 90 A
Carol 78 B+
Dave 92 A-
Alternatively, use .join() for a more concise syntax:
import numpy as np
import pandas as pd
df1 = pd.DataFrame(
{"score": [85, 90, 78, 92]},
index=["Alice", "Bob", "Carol", "Dave"]
)
df2 = pd.DataFrame(
{"grade": ["A", "B+", "A-", "B"]},
index=["Bob", "Carol", "Dave", "Eve"]
)
merged = df1.join(df2, how='inner')
print(merged)
Output:
score grade
Bob 90 A
Carol 78 B+
Dave 92 A-
Finding Indices Unique to Each DataFrame
Beyond common indices, you might also want to find indices that exist in only one DataFrame:
import numpy as np
import pandas as pd
df1 = pd.DataFrame(
{"score": [85, 90, 78, 92]},
index=["Alice", "Bob", "Carol", "Dave"]
)
df2 = pd.DataFrame(
{"grade": ["A", "B+", "A-", "B"]},
index=["Bob", "Carol", "Dave", "Eve"]
)
common = df1.index.intersection(df2.index)
only_in_df1 = df1.index.difference(df2.index)
only_in_df2 = df2.index.difference(df1.index)
print(f"Common: {common.tolist()}")
print(f"Only in df1: {only_in_df1.tolist()}")
print(f"Only in df2: {only_in_df2.tolist()}")
Output:
Common: ['Bob', 'Carol', 'Dave']
Only in df1: ['Alice']
Only in df2: ['Eve']
Comparing Values at Common Indices
import numpy as np
import pandas as pd
df1 = pd.DataFrame(
{"score": [85, 90, 78, 92]},
index=["Alice", "Bob", "Carol", "Dave"]
)
df2 = pd.DataFrame(
{"grade": ["A", "B+", "A-", "B"]},
index=["Bob", "Carol", "Dave", "Eve"]
)
common = df1.index.intersection(df2.index)
comparison = pd.DataFrame({
'score': df1.loc[common, 'score'],
'grade': df2.loc[common, 'grade']
})
print(comparison)
Output:
score grade
Bob 90 A
Carol 78 B+
Dave 92 A-
Working with Numeric Indices
The same methods work with numeric or datetime indices:
import pandas as pd
df1 = pd.DataFrame({"val": [10, 20, 30]}, index=[1, 2, 3])
df2 = pd.DataFrame({"val": [40, 50, 60]}, index=[2, 3, 4])
common = df1.index.intersection(df2.index)
print(f"Common indices: {common.tolist()}")
Output:
Common indices: [2, 3]
Finding Common Indices Across Multiple DataFrames
To find indices shared by three or more DataFrames, chain .intersection() or use functools.reduce():
import pandas as pd
from functools import reduce
df1 = pd.DataFrame({"A": [1, 2, 3]}, index=["a", "b", "c"])
df2 = pd.DataFrame({"B": [4, 5, 6]}, index=["b", "c", "d"])
df3 = pd.DataFrame({"C": [7, 8, 9]}, index=["c", "d", "e"])
# Chaining intersection
common = df1.index.intersection(df2.index).intersection(df3.index)
print(f"Common across all 3: {common.tolist()}")
# Using reduce for any number of DataFrames
dataframes = [df1, df2, df3]
common = reduce(lambda a, b: a.intersection(b), [df.index for df in dataframes])
print(f"Common (reduce): {common.tolist()}")
Output:
Common across all 3: ['c']
Common (reduce): ['c']
Method Comparison
| Method | Returns | Preserves Order | Best For |
|---|---|---|---|
.intersection() | Pandas Index | ✅ | General use (recommended) |
& operator | Pandas Index | ✅ | Concise syntax |
set() & set() | Python set | ❌ | Quick checks, non-Pandas workflows |
np.intersect1d() | NumPy array (sorted) | Sorted | NumPy-heavy workflows |
.isin() | Boolean mask | ✅ | Direct filtering |
Conclusion
Finding common indices between Pandas DataFrames is straightforward with the .intersection() method, which returns a Pandas Index of shared labels.
- For quick set operations, use the
&operator or Python'sset()intersection. - Once you have the common indices, you can filter, merge, or compare DataFrames efficiently using
.loc[],pd.merge(), or.join(). - For multiple DataFrames, chain
.intersection()calls or usefunctools.reduce()to find indices shared across all of them.