
- 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 a tuple with the length of each level from MultiIndex
To get a tuple with the length of each level from MultiIndex, use the MultiIndex.levshape 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() uis used to create a Multiindex −
multiIndex = pd.MultiIndex.from_arrays(arrays, names=('ranks', 'student'))
Get a tuple with the length of each level −
print("\nThe tuple with the length of each level in a Multi-index...\n",multiIndex.levshape)
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() uis 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 integer number of levels in Multiindex print("\nThe number of levels in Multi-index...\n",multiIndex.nlevels) # get the levels in Multiindex print("\nThe levels in Multi-index...\n",multiIndex.levels) # get a tuple with the length of each level print("\nThe tuple with the length of each level in a Multi-index...\n",multiIndex.levshape)
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 number of levels in Multi-index... 2 The levels in Multi-index... [[1, 2, 3, 4, 5], ['Chris', 'Jacob', 'John', 'Keiron', 'Tim']] The tuple with the length of each level in a Multi-index... (5, 5)
- Related Articles
- Python Pandas - Return MultiIndex with requested level removed
- Python Pandas - Getting values from a specific level in Multiindex
- Python Pandas - Return MultiIndex with requested level removed using the level name
- Python Pandas - Get the codes (location of each label) in MultiIndex
- Python Pandas - Get location for a label or a tuple of labels in a MultiIndex
- Python Pandas - Return MultiIndex with multiple levels removed using the level names
- Python Pandas - Get location and sliced index for requested label/ level in a MultiIndex
- Python Pandas - Get the levels in MultiIndex
- Python Pandas - How to Sort MultiIndex at a specific level
- Python Pandas - Get the Names of levels in MultiIndex
- Python Pandas - Rearrange levels using level name in MultiIndex
- Python Pandas - Set only a single new specific level using the level name in a MultiIndex
- Python Pandas - How to create a MultiIndex with names of each of the index levels
- Python Pandas - Convert a MultiIndex to an Index of Tuples containing the level values
- Python Pandas - Create a DataFrame with the levels of the MultiIndex as columns and substitute index level names

Advertisements