0
0
PandasHow-ToBeginner · 3 min read

How to Sort MultiIndex in pandas: Syntax and Examples

To sort a MultiIndex in pandas, use the sort_index() method on your DataFrame or Series. You can sort by levels using the level parameter and control ascending or descending order with ascending.
📐

Syntax

The main method to sort a MultiIndex in pandas is sort_index(). You can specify which level(s) to sort by using the level parameter. The ascending parameter controls the sort order, and inplace decides if the sorting modifies the original object or returns a new one.

  • level: int, str, or list - specifies which index level(s) to sort.
  • ascending: bool or list of bools - True for ascending, False for descending.
  • inplace: bool - if True, sorts the object in place.
python
DataFrame.sort_index(level=None, ascending=True, inplace=False)
💻

Example

This example shows how to create a DataFrame with a MultiIndex and sort it by different levels of the index.

python
import pandas as pd

# Create sample data
arrays = [['b', 'b', 'a', 'a'], [2, 1, 2, 1]]
index = pd.MultiIndex.from_arrays(arrays, names=['letter', 'number'])
data = {'value': [4, 3, 2, 1]}
df = pd.DataFrame(data, index=index)

# Original DataFrame
print('Original DataFrame:')
print(df)

# Sort by first level (letter) ascending and second level (number) ascending
sorted_df = df.sort_index(level=['letter', 'number'], ascending=[True, True])
print('\nSorted by letter and number ascending:')
print(sorted_df)

# Sort by first level descending
sorted_df_desc = df.sort_index(level='letter', ascending=False)
print('\nSorted by letter descending:')
print(sorted_df_desc)
Output
Original DataFrame: value letter number b 2 4 1 3 a 2 2 1 1 Sorted by letter and number ascending: value letter number a 1 1 2 2 b 1 3 2 4 Sorted by letter descending: value letter number b 2 4 1 3 a 2 2 1 1
⚠️

Common Pitfalls

One common mistake is forgetting to specify the level parameter, which sorts all index levels by default and may not give the desired order. Another is mixing ascending orders when sorting multiple levels without matching the length of the ascending list to the level list.

Also, sorting without assigning or using inplace=True will not change the original DataFrame.

python
import pandas as pd

arrays = [['b', 'b', 'a', 'a'], [2, 1, 2, 1]]
index = pd.MultiIndex.from_arrays(arrays, names=['letter', 'number'])
data = {'value': [4, 3, 2, 1]}
df = pd.DataFrame(data, index=index)

# Wrong: ascending list length does not match level list length
try:
    df.sort_index(level=['letter', 'number'], ascending=[True])
except ValueError as e:
    print(f'Error: {e}')

# Right: matching lengths
sorted_df = df.sort_index(level=['letter', 'number'], ascending=[True, False])
print(sorted_df)
Output
Error: Length of ascending (1) != length of level (2) value letter number a 2 2 1 1 b 2 4 1 3
📊

Quick Reference

Remember these tips when sorting MultiIndex in pandas:

  • Use sort_index(level=...) to specify which index levels to sort.
  • Match the length of ascending list to the level list when sorting multiple levels.
  • Use inplace=True to modify the original DataFrame directly.
  • Sorting without level sorts all index levels in order.

Key Takeaways

Use DataFrame.sort_index(level=...) to sort specific MultiIndex levels.
Ensure ascending parameter matches the number of levels when sorting multiple levels.
Use inplace=True to sort the DataFrame without creating a new object.
Sorting without specifying level sorts all index levels by default.
Common errors include mismatched ascending list length and forgetting to assign sorted result.