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 - Get location for a sequence of labels in a MultiIndex
To get location for a sequence of labels in a MultiIndex, use the MultiIndex.get_locs() method in Pandas. This method returns an array of integer positions where the specified label appears in the MultiIndex.
Creating a MultiIndex
First, let's create a MultiIndex using from_arrays() ?
import pandas as pd
# Create MultiIndex from two arrays
multiIndex = pd.MultiIndex.from_arrays([list('pqrrst'), list('kytssp')])
# Display the MultiIndex
print("The MultiIndex...")
print(multiIndex)
The MultiIndex...
MultiIndex([('p', 'k'),
('q', 'y'),
('r', 't'),
('r', 's'),
('s', 's'),
('t', 'p')],
)
Getting Locations with get_locs()
The get_locs() method returns the positions where a label appears. Let's find all locations of label 's' ?
import pandas as pd
# Create MultiIndex
multiIndex = pd.MultiIndex.from_arrays([list('pqrrst'), list('kytssp')])
# Get locations for label 's' in the first level
print("Locations of 's' in MultiIndex:")
locations = multiIndex.get_locs('s')
print(locations)
# Show the levels for reference
print("\nThe levels in MultiIndex:")
print(multiIndex.levels)
Locations of 's' in MultiIndex: [4] The levels in MultiIndex: [['p', 'q', 'r', 's', 't'], ['k', 'p', 's', 't', 'y']]
Finding Multiple Occurrences
Let's create an example where a label appears multiple times to better understand get_locs() ?
import pandas as pd
# Create MultiIndex with repeated labels
multiIndex = pd.MultiIndex.from_arrays([list('abacad'), list('xyzxyz')])
print("MultiIndex with repeated labels:")
print(multiIndex)
# Get locations for label 'a' (appears multiple times)
print("\nLocations of 'a':")
locations = multiIndex.get_locs('a')
print(locations)
# Verify by showing actual values at those positions
print("\nValues at those locations:")
for loc in locations:
print(f"Position {loc}: {multiIndex[loc]}")
MultiIndex with repeated labels:
MultiIndex([('a', 'x'),
('b', 'y'),
('a', 'z'),
('c', 'x'),
('a', 'y'),
('d', 'z')],
)
Locations of 'a':
[0 2 4]
Values at those locations:
Position 0: ('a', 'x')
Position 2: ('a', 'z')
Position 4: ('a', 'y')
Key Points
- The
get_locs()method searches the first level of the MultiIndex by default - It returns a NumPy array of integer positions
- If a label appears multiple times, all positions are returned
- If a label doesn't exist, it raises a
KeyError
Conclusion
Use MultiIndex.get_locs() to find all positions where a specific label appears in a MultiIndex. This method is useful for locating and accessing multiple rows that share the same index label.
Advertisements
