
- 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 - Return vector of label values using integer position of the level in the MultiIndex
To return vector of label values using integer position of the level in the MultiIndex, use the MultiIndex.get_level_values() method in Pandas. Set the level as an argument.
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('strvwx')],names=['One', 'Two'])
Display the MultiIndex −
print("The MultiIndex...\n",multiIndex)
Get level values at level 0 −
print("\nLevel values at level 0...\n",multiIndex.get_level_values(0))
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('strvwx')],names=['One', 'Two']) # display the MultiIndex print("The MultiIndex...\n",multiIndex) # get the levels in MultiIndex print("\nThe levels in MultiIndex...\n",multiIndex.levels) # get level values at level 0 print("\nLevel values at level 0...\n",multiIndex.get_level_values(0))
Output
This will produce the following output −
The MultiIndex... MultiIndex([('p', 's'), ('q', 't'), ('r', 'r'), ('r', 'v'), ('s', 'w'), ('s', 'x')], names=['One', 'Two']) The levels in MultiIndex... [['p', 'q', 'r', 's'], ['r', 's', 't', 'v', 'w', 'x']] Level values at level 0... Index(['p', 'q', 'r', 'r', 's', 's'], dtype='object', name='One')
- Related Articles
- 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 - Return MultiIndex with requested level removed using the level name
- Python Pandas - Return MultiIndex with multiple levels removed using the level names
- Python Pandas - Return MultiIndex with requested level removed
- Python Pandas - Get the codes (location of each label) in MultiIndex
- Python Pandas - Getting values from a specific level in Multiindex
- Python Pandas - Convert a MultiIndex to an Index of Tuples containing the level values
- Python Pandas - Rearrange levels using level name in MultiIndex
- Python Pandas - Get location and sliced index for requested label/ level in a MultiIndex
- Python Pandas - Get the Integer number of levels in this MultiIndex
- Python Pandas - Set only a single new specific level using the level name in a MultiIndex
- Python Pandas - Get a tuple with the length of each level from MultiIndex
- Python Pandas - Return the int position of the smallest value in the Index
- Python Pandas - Return the int position of the largest value in the Index

Advertisements