
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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.
At first, import the required libraries −
import pandas as pd
MultiIndex is a multi-level, or hierarchical, index object for pandas objects −
multiIndex = pd.MultiIndex.from_arrays([list('pqrrss'), list('stuvwx')])
Display the MultiIndex −
print("The MultiIndex...\n",multiIndex)
Get the location −
print("\nGet the locations in MultiIndex...\n",multiIndex.get_loc('s'))
Example
Following is the code −
import pandas as pd # MultiIndex is a multi-level, or hierarchical, index object for pandas objects multiIndex = pd.MultiIndex.from_arrays([list('pqrrss'), list('stuvwx')]) # display the MultiIndex print("The MultiIndex...\n",multiIndex) # get the levels in MultiIndex print("\nThe levels in MultiIndex...\n",multiIndex.levels) # Get the location print("\nGet the locations in MultiIndex...\n",multiIndex.get_loc('s'))
Output
This will produce the following output −
The MultiIndex... MultiIndex([('p', 's'), ('q', 't'), ('r', 'u'), ('r', 'v'), ('s', 'w'), ('s', 'x')], ) The levels in MultiIndex... [['p', 'q', 'r', 's'], ['s', 't', 'u', 'v', 'w', 'x']] Get the locations in MultiIndex... slice(4, 6, None)
- Related Articles
- Python Pandas - Get location for a sequence of labels in a MultiIndex
- Python Pandas - Get location and sliced index for requested label/ level in a MultiIndex
- Python Pandas - Get the codes (location of each label) in MultiIndex
- Python Pandas - Get integer location for requested label
- Python Pandas IntervalIndex - Get integer location for requested label
- Python Pandas - Return vector of label values for requested level in a MultiIndex
- Python Pandas - Get a tuple with the length of each level from MultiIndex
- Python Pandas - Swap levels of a MultiIndex
- Python Pandas - Get the levels in MultiIndex
- Python Pandas - Set levels in a MultiIndex
- Python Pandas - Get the Names of levels in MultiIndex
- Python Pandas - Get location and sliced index for requested label/ level but do not drop the level
- Python Pandas - Return vector of label values using level name in the MultiIndex
- Python Pandas - Get integer location for requested label and find the previous index value if no exact match
- Python Pandas - Get integer location for requested label and find the nearest index value if no exact match

Advertisements