Python Pandas - Set levels in a MultiIndex

A MultiIndex in Pandas is a hierarchical index that allows multiple levels of indexing. The set_levels() method replaces the current level values with new ones while preserving the structure.

Creating a MultiIndex

First, let's create a MultiIndex from arrays and examine its structure ?

import pandas as pd

# Create arrays for MultiIndex levels
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 New Levels

Use set_levels() to replace existing level values with new ones ?

import pandas as pd

arrays = [[1, 2, 3, 4], ['John', 'Tim', 'Jacob', 'Chris']]
multiIndex = pd.MultiIndex.from_arrays(arrays, names=('ranks', 'student'))

# Set new levels - replaces values but keeps structure
new_multiIndex = multiIndex.set_levels([['A', 'B', 'C', 'D'], ['Alice', 'Bob', 'Charlie', 'David']])

print("MultiIndex with new levels:")
print(new_multiIndex)
print("\nNew level values:")
print(new_multiIndex.levels)
MultiIndex with new levels:
MultiIndex([('A', 'Alice'),
            ('B',   'Bob'),
            ('C', 'Charlie'),
            ('D',  'David')],
           names=['ranks', 'student'])

New level values:
[['A', 'B', 'C', 'D'], ['Alice', 'Bob', 'Charlie', 'David']]

Setting Levels for Specific Level Only

You can also set levels for a specific level using the level parameter ?

import pandas as pd

arrays = [[1, 2, 3, 4], ['John', 'Tim', 'Jacob', 'Chris']]
multiIndex = pd.MultiIndex.from_arrays(arrays, names=('ranks', 'student'))

# Set levels only for the first level (ranks)
updated_multiIndex = multiIndex.set_levels(['First', 'Second', 'Third', 'Fourth'], level=0)

print("Updated MultiIndex (first level only):")
print(updated_multiIndex)
Updated MultiIndex (first level only):
MultiIndex([('First', 'John'),
            ('Second',  'Tim'),
            ('Third', 'Jacob'),
            ('Fourth', 'Chris')],
           names=['ranks', 'student'])

Key Points

  • set_levels() returns a new MultiIndex object; it doesn't modify the original
  • New level values must have the same length as existing levels
  • The structure and codes remain unchanged, only level values are replaced
  • Use the level parameter to update specific levels only

Conclusion

The set_levels() method allows you to replace MultiIndex level values while preserving the hierarchical structure. Use it when you need to rename or update level values without changing the index organization.

Updated on: 2026-03-26T17:22:01+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements