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 - Rearrange levels in MultiIndex
To rearrange levels in MultiIndex, use the MultiIndex.reorder_levels() method in Pandas. This method allows you to change the order of hierarchical index levels by specifying a new arrangement.
What is MultiIndex?
MultiIndex is a multi-level, or hierarchical, index object for pandas objects that enables multiple levels of indexing on an axis.
Creating a MultiIndex
First, let's create a MultiIndex from arrays ?
import pandas as pd
# Create arrays for different levels
arrays = [[2, 4, 3, 1], ['Peter', 'Chris', 'Andy', 'Jacob'], [50, 30, 40, 70]]
# Create MultiIndex with named levels
multiIndex = pd.MultiIndex.from_arrays(arrays, names=('rank', 'student', 'points'))
print("Original MultiIndex:")
print(multiIndex)
Original MultiIndex:
MultiIndex([(2, 'Peter', 50),
(4, 'Chris', 30),
(3, 'Andy', 40),
(1, 'Jacob', 70)],
names=['rank', 'student', 'points'])
Viewing MultiIndex Levels
You can examine the individual levels of the MultiIndex ?
import pandas as pd
arrays = [[2, 4, 3, 1], ['Peter', 'Chris', 'Andy', 'Jacob'], [50, 30, 40, 70]]
multiIndex = pd.MultiIndex.from_arrays(arrays, names=('rank', 'student', 'points'))
print("The levels in MultiIndex:")
print(multiIndex.levels)
The levels in MultiIndex: [[1, 2, 3, 4], ['Andy', 'Chris', 'Jacob', 'Peter'], [30, 40, 50, 70]]
Reordering MultiIndex Levels
Use reorder_levels() with the order parameter to specify the new level arrangement. The order is specified using level indices ?
import pandas as pd
arrays = [[2, 4, 3, 1], ['Peter', 'Chris', 'Andy', 'Jacob'], [50, 30, 40, 70]]
multiIndex = pd.MultiIndex.from_arrays(arrays, names=('rank', 'student', 'points'))
print("Original MultiIndex:")
print(multiIndex)
print("\nReordered MultiIndex (points, rank, student):")
reordered = multiIndex.reorder_levels(order=[2, 0, 1])
print(reordered)
Original MultiIndex:
MultiIndex([(2, 'Peter', 50),
(4, 'Chris', 30),
(3, 'Andy', 40),
(1, 'Jacob', 70)],
names=['rank', 'student', 'points'])
Reordered MultiIndex (points, rank, student):
MultiIndex([(50, 2, 'Peter'),
(30, 4, 'Chris'),
(40, 3, 'Andy'),
(70, 1, 'Jacob')],
names=['points', 'rank', 'student'])
Syntax
MultiIndex.reorder_levels(order)
Parameters
| Parameter | Description |
|---|---|
order |
List of integers representing the new order of levels (0-indexed) |
Key Points
- Level indices start from 0 (first level is 0, second is 1, etc.)
- The method returns a new MultiIndex with reordered levels
- Level names are preserved and reordered accordingly
- Original MultiIndex remains unchanged
Conclusion
The reorder_levels() method provides a simple way to rearrange MultiIndex levels by specifying the desired order using level indices. This is useful for reorganizing hierarchical data structures in pandas.
