- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 - Set only a single new specific level in a MultiIndex
To set only a single new specific level in a MultiIndex, use the MultiIndex.set_levels() method with parameter level. 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], ['John', 'Tim', 'Jacob', 'Chris']]
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'))
We have set a new single specific level using the "level" parameter −
print("\nSet a new level in Multi-index...\n",multiIndex.set_levels(['p', 'q', 'r', 's'], level = 0))
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], ['John', 'Tim', 'Jacob', 'Chris']] # 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) # set the level in MultiIndex # We have set a new single specific level using the "level" parameter print("\nSet a new level in Multi-index...\n",multiIndex.set_levels(['p', 'q', 'r', 's'], level = 0))
Output
This will produce the following output −
The Multi-index... MultiIndex([(1, 'John'), (2, 'Tim'), (3, 'Jacob'), (4, 'Chris')], names=['ranks', 'student']) The levels in Multi-index... [[1, 2, 3, 4], ['Chris', 'Jacob', 'John', 'Tim']] Set a new level in Multi-index... MultiIndex([('p', 'John'), ('q', 'Tim'), ('r', 'Jacob'), ('s', 'Chris')], names=['ranks', 'student'])
- Related Articles
- Python Pandas - Set only a single new specific level using the level name in a MultiIndex
- Python Pandas - Getting values from a specific level in Multiindex
- Python Pandas - How to Sort MultiIndex at a specific level
- Python Pandas - How to Sort MultiIndex at a specific level in descending order
- Python Pandas - Set levels in a MultiIndex
- Python Pandas - Return MultiIndex with requested level removed
- Python Pandas - Rearrange levels using level name in MultiIndex
- Python Pandas - Return vector of label values for requested level in a MultiIndex
- Python - Drop specific rows from multiindex Pandas Dataframe
- Python Pandas - Return MultiIndex with requested level removed using the level name
- Python – Stacking a single-level column with Pandas stack()?
- Python Pandas - Get location and sliced index for requested label/ level in a MultiIndex
- Python Pandas - Get a tuple with the length of each level from MultiIndex
- Python Pandas and Numpy - Concatenate multiindex into single index
- Python Pandas - Convert a MultiIndex to an Index of Tuples containing the level values

Advertisements