
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Pandas - Get location and sliced index for requested label/ level in a MultiIndex
To get location and sliced index for requested label/ level in a MultiIndex, use the get_loc_level() 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('strvwx')],names=['One', 'Two'])
Display the MultiIndex −
print("The MultiIndex...\n",multiIndex)
Get the location and sliced index −
print("\nGet the location and sliced index...\n",multiIndex.get_loc_level('r'))
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 the location and sliced index print("\nGet the location and sliced index...\n",multiIndex.get_loc_level('r'))
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']] Get the location and sliced index... (slice(2, 4, None), Index(['r', 'v'], dtype='object', name='Two'))
- Related Questions & Answers
- 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
- Python Pandas IntervalIndex - Get integer location for requested label
- Python Pandas - Return vector of label values for requested level in a MultiIndex
- Python Pandas - Return MultiIndex with requested level removed
- 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
- Python Pandas - Get location for a label or a tuple of labels in a MultiIndex
- Python Pandas - Get the codes (location of each label) in MultiIndex
- Python Pandas - Return MultiIndex with requested level removed using the level name
- Python Pandas - Get location for a sequence of labels in a MultiIndex
- Python Pandas - Return vector of label values using level name in the MultiIndex
- Python Pandas - Convert a MultiIndex to an Index of Tuples containing the level values
- Python Pandas - Get a tuple with the length of each level from MultiIndex
- Python Pandas - Getting values from a specific level in Multiindex
Advertisements