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
Python Pandas - Get location for a label or a tuple of labels in a MultiIndex
To get location for a label or a tuple of labels in a MultiIndex, use the MultiIndex.get_loc() method in Pandas. This method returns the location of a label in the MultiIndex, which can be an integer, slice, or boolean array depending on the label's occurrence.
Basic Syntax
MultiIndex.get_loc(key)
Where key can be a single label or a tuple of labels for multi-level indexing.
Creating a MultiIndex
First, let's create a MultiIndex from arrays ?
import pandas as pd
# Create MultiIndex from arrays
multiIndex = pd.MultiIndex.from_arrays([list('pqrrss'), list('stuvwx')])
print("The MultiIndex...")
print(multiIndex)
The MultiIndex...
MultiIndex([('p', 's'),
('q', 't'),
('r', 'u'),
('r', 'v'),
('s', 'w'),
('s', 'x')],
)
Getting Location for Single Label
When a label appears multiple times, get_loc() returns a slice object ?
import pandas as pd
multiIndex = pd.MultiIndex.from_arrays([list('pqrrss'), list('stuvwx')])
# Get location for label 's' (appears at indices 4 and 5)
location = multiIndex.get_loc('s')
print("Location for 's':", location)
print("Type:", type(location))
Location for 's': slice(4, 6, None) Type: <class 'slice'>
Getting Location for Tuple Labels
For exact tuple matches, the method returns the specific integer index ?
import pandas as pd
multiIndex = pd.MultiIndex.from_arrays([list('pqrrss'), list('stuvwx')])
# Get location for specific tuple
location = multiIndex.get_loc(('r', 'u'))
print("Location for ('r', 'u'):", location)
# Another tuple example
location2 = multiIndex.get_loc(('s', 'w'))
print("Location for ('s', 'w'):", location2)
Location for ('r', 'u'): 2
Location for ('s', 'w'): 4
Practical Example with DataFrame
Here's how to use get_loc() with a DataFrame having MultiIndex ?
import pandas as pd
# Create DataFrame with MultiIndex
index = pd.MultiIndex.from_arrays([
['A', 'A', 'B', 'B', 'C', 'C'],
['X', 'Y', 'X', 'Y', 'X', 'Y']
], names=['Level1', 'Level2'])
df = pd.DataFrame({'Value': [10, 20, 30, 40, 50, 60]}, index=index)
print("DataFrame:")
print(df)
# Get location for level 'B'
location = df.index.get_loc('B')
print("\nLocation for 'B':", location)
# Get location for specific tuple
location_tuple = df.index.get_loc(('B', 'Y'))
print("Location for ('B', 'Y'):", location_tuple)
DataFrame:
Value
Level1 Level2
A X 10
Y 20
B X 30
Y 40
C X 50
Y 60
Location for 'B': slice(2, 4, None)
Location for ('B', 'Y'): 3
Return Types
| Scenario | Return Type | Example |
|---|---|---|
| Single occurrence | Integer | Exact tuple match |
| Multiple occurrences | Slice object | Partial label match |
| Boolean indexing needed | Boolean array | Complex conditions |
Conclusion
The MultiIndex.get_loc() method efficiently finds locations of labels in hierarchical indices. It returns integers for exact matches and slice objects for multiple occurrences, making it versatile for MultiIndex navigation.
