
- 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 the codes (location of each label) in MultiIndex
To get the codes (location of each label) in MultiIndex, use the MultiIndex.codes property in Pandas. At first, import the required libraries −
import pandas as pd
MultiIndex is a multi-level, or hierarchical, index object for pandas objects. Create arrays −
arrays = [[1, 2, 3, 4, 5], ['John', 'Tim', 'Jacob', 'Chris', 'Keiron']]
The "names" parameter sets the names for each of the index levels: The from_arrays() is used to create a Multiindex −
multiIndex = pd.MultiIndex.from_arrays(arrays, names=('ranks', 'student'))
Get the location of each label in Multiindex −
print("\nThe location of each label in Multi-index...\n",multiIndex.codes)
Example
Following is the code −
import pandas as pd # MultiIndex is a multi-level, or hierarchical, index object for pandas objects # Create arrays arrays = [[1, 2, 3, 4, 5], ['John', 'Tim', 'Jacob', 'Chris', 'Keiron']] # The "names" parameter sets the names for each of the index levels # The from_arrays() is used to create a Multiindex multiIndex = pd.MultiIndex.from_arrays(arrays, names=('ranks', 'student')) # display the Multiindex print("The Multi-index...\n",multiIndex) # get the levels in Multiindex print("\nThe levels in Multi-index...\n",multiIndex.levels) # get the location of each label in Multiindex print("\nThe location of each label in Multi-index...\n",multiIndex.codes)
Output
This will produce the following output −
The Multi-index... MultiIndex([(1, 'John'), (2, 'Tim'), (3, 'Jacob'), (4, 'Chris'), (5, 'Keiron')], names=['ranks', 'student']) The levels in Multi-index... [[1, 2, 3, 4, 5], ['Chris', 'Jacob', 'John', 'Keiron', 'Tim']] The location of each label in Multi-index... [[0, 1, 2, 3, 4], [2, 4, 1, 0, 3]]
- Related Articles
- Python Pandas - Get location for a label or a tuple of labels in a MultiIndex
- Python Pandas - Get location and sliced index for requested label/ level in a MultiIndex
- Python Pandas - Get integer location for requested label
- Python Pandas - Get location for a sequence of labels in a MultiIndex
- Python Pandas IntervalIndex - Get integer location for requested label
- Python Pandas - Get the levels in MultiIndex
- Python Pandas - Get the Names of levels in MultiIndex
- Python Pandas - Get a tuple with the length of each level from MultiIndex
- Python Pandas - Return vector of label values using level name in the MultiIndex
- Python Pandas - Return vector of label values for requested level in a MultiIndex
- Python Pandas - Get the Integer number of levels in this MultiIndex
- Python Pandas - Return vector of label values using integer position of the level in the MultiIndex
- Python Pandas CategoricalIndex - Get the category codes of this categorical
- Python Pandas - Get location and sliced index for requested label/ level but do not drop the level
- Python Pandas - Get integer location for requested label and find the previous index value if no exact match

Advertisements