Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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 the level parameter. This allows you to replace values in a specific level while keeping other levels unchanged.
Syntax
MultiIndex.set_levels(levels, level=None, inplace=False, verify_integrity=True)
Parameters
levels: New level values to set
level: Level position (int) or name (str) to modify
inplace: Whether to modify the original MultiIndex
verify_integrity: Check that new levels are valid
Creating a MultiIndex
First, let's create a MultiIndex with student ranks and names ?
import pandas as pd
# Create arrays for MultiIndex
arrays = [[1, 2, 3, 4], ['John', 'Tim', 'Jacob', 'Chris']]
# Create MultiIndex with named levels
multiIndex = pd.MultiIndex.from_arrays(arrays, names=('ranks', 'student'))
print("Original MultiIndex:")
print(multiIndex)
print("\nCurrent levels:")
print(multiIndex.levels)
Original MultiIndex:
MultiIndex([(1, 'John'),
(2, 'Tim'),
(3, 'Jacob'),
(4, 'Chris')],
names=['ranks', 'student'])
Current levels:
[[1, 2, 3, 4], ['Chris', 'Jacob', 'John', 'Tim']]
Setting a New Level by Position
Replace the first level (position 0) with new values ?
import pandas as pd
arrays = [[1, 2, 3, 4], ['John', 'Tim', 'Jacob', 'Chris']]
multiIndex = pd.MultiIndex.from_arrays(arrays, names=('ranks', 'student'))
# Set new values for level 0 (ranks)
new_multiIndex = multiIndex.set_levels(['A', 'B', 'C', 'D'], level=0)
print("MultiIndex with new level 0:")
print(new_multiIndex)
MultiIndex with new level 0:
MultiIndex([('A', 'John'),
('B', 'Tim'),
('C', 'Jacob'),
('D', 'Chris')],
names=['ranks', 'student'])
Setting a New Level by Name
You can also specify the level by its name instead of position ?
import pandas as pd
arrays = [[1, 2, 3, 4], ['John', 'Tim', 'Jacob', 'Chris']]
multiIndex = pd.MultiIndex.from_arrays(arrays, names=('ranks', 'student'))
# Set new values for 'student' level by name
new_multiIndex = multiIndex.set_levels(['Alice', 'Bob', 'Carol', 'Dave'], level='student')
print("MultiIndex with new student level:")
print(new_multiIndex)
MultiIndex with new student level:
MultiIndex([(1, 'Alice'),
(2, 'Bob'),
(3, 'Carol'),
(4, 'Dave')],
names=['ranks', 'student'])
Key Points
- The
set_levels()method creates a new MultiIndex; it doesn't modify the original - New level values must match the number of unique values in the target level
- Use
levelparameter to specify which level to modify (by position or name) - The method preserves the structure and other levels of the MultiIndex
Conclusion
The set_levels() method provides a clean way to replace values in a specific MultiIndex level. Use the level parameter to target the desired level by position or name while keeping other levels intact.
