Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Python Pandas - Form the intersection of two Index objects
To form the intersection of two Index objects in Pandas, use the intersection() method. This method returns a new Index containing only the elements that appear in both indexes.
Syntax
index1.intersection(index2, sort=False)
Parameters
-
other− Another Index object to find intersection with -
sort− Boolean, whether to sort the result (default: False)
Example with No Common Elements
Let's start with two indexes that have no common elements ?
import pandas as pd
# Creating two Pandas indexes with no common elements
index1 = pd.Index([10, 20, 30, 40, 50])
index2 = pd.Index([80, 65, 60, 70, 55])
# Display the indexes
print("Index1:", index1)
print("Index2:", index2)
# Perform intersection
result = index1.intersection(index2)
print("\nIntersection:", result)
print("Number of common elements:", len(result))
Index1: Int64Index([10, 20, 30, 40, 50], dtype='int64') Index2: Int64Index([80, 65, 60, 70, 55], dtype='int64') Intersection: Int64Index([], dtype='int64') Number of common elements: 0
Example with Common Elements
Now let's see an example where indexes have some common elements ?
import pandas as pd
# Creating indexes with common elements
index1 = pd.Index(['A', 'B', 'C', 'D', 'E'])
index2 = pd.Index(['C', 'D', 'F', 'G', 'H'])
print("Index1:", index1)
print("Index2:", index2)
# Find intersection
intersection = index1.intersection(index2)
print("\nCommon elements:", intersection)
# With sorting
sorted_intersection = index1.intersection(index2, sort=True)
print("Sorted intersection:", sorted_intersection)
Index1: Index(['A', 'B', 'C', 'D', 'E'], dtype='object') Index2: Index(['C', 'D', 'F', 'G', 'H'], dtype='object') Common elements: Index(['C', 'D'], dtype='object') Sorted intersection: Index(['C', 'D'], dtype='object')
Example with Numeric Data
Here's an example with numeric indexes that share common values ?
import pandas as pd
# Creating numeric indexes with overlapping values
numbers1 = pd.Index([1, 3, 5, 7, 9, 11])
numbers2 = pd.Index([2, 3, 6, 7, 10, 11])
print("Numbers1:", numbers1)
print("Numbers2:", numbers2)
# Find common numbers
common = numbers1.intersection(numbers2)
print("\nCommon numbers:", common)
print("Count of common elements:", len(common))
Numbers1: Int64Index([1, 3, 5, 7, 9, 11], dtype='int64') Numbers2: Int64Index([2, 3, 6, 7, 10, 11], dtype='int64') Common numbers: Int64Index([3, 7, 11], dtype='int64') Count of common elements: 3
Key Points
- Returns a new Index object containing only common elements
- If no elements are common, returns an empty Index
- Original indexes remain unchanged
- Works with any Index type (numeric, string, datetime, etc.)
- Use
sort=Trueto get sorted results
Conclusion
The intersection() method is useful for finding common elements between two Index objects. It returns an empty Index when no elements are shared, making it perfect for data analysis and comparison tasks.
Advertisements
