0
0
PandasHow-ToBeginner · 3 min read

How to Flatten Multi-Index in pandas: Simple Guide

To flatten a pandas multi-index DataFrame, use reset_index() to convert index levels into columns. Alternatively, combine index levels into single strings with map or join on the index names.
📐

Syntax

The main method to flatten a multi-index in pandas is reset_index(). It moves the index levels into regular columns, making the DataFrame easier to work with.

Another way is to combine the multi-index levels into one by joining their names with a separator using map and join.

python
df.reset_index(level=None, drop=False, inplace=False, col_level=0, col_fill='')
💻

Example

This example shows how to flatten a multi-index DataFrame by resetting the index and by joining index levels into a single level.

python
import pandas as pd

# Create a sample multi-index DataFrame
data = {"value": [10, 20, 30, 40]}
index = pd.MultiIndex.from_tuples(
    [("A", 1), ("A", 2), ("B", 1), ("B", 2)],
    names=["letter", "number"]
)
df = pd.DataFrame(data, index=index)

# Flatten by resetting index
flat_df = df.reset_index()

# Flatten by joining index levels into one string
flat_index = df.index.map(lambda x: f"{x[0]}_{x[1]}")
df_flat_index = df.copy()
df_flat_index.index = flat_index

flat_df, df_flat_index
Output
letter number value 0 A 1 10 1 A 2 20 2 B 1 30 3 B 2 40 value A_1 10 A_2 20 B_1 30 B_2 40
⚠️

Common Pitfalls

One common mistake is forgetting that reset_index() returns a new DataFrame by default and does not change the original unless inplace=True is set.

Another pitfall is losing the original index if drop=True is used unintentionally.

python
import pandas as pd

# Sample multi-index DataFrame
data = {"value": [1, 2]}
index = pd.MultiIndex.from_tuples([("X", "a"), ("Y", "b")], names=["first", "second"])
df = pd.DataFrame(data, index=index)

# Wrong: reset_index without assignment or inplace
_ = df.reset_index()
print(df)  # Original df unchanged

# Right: assign back or use inplace
flat_df = df.reset_index()
print(flat_df)

# Or inplace
# df.reset_index(inplace=True)
# print(df)
Output
value first second X a 1 Y b 2 first second value 0 X a 1 1 Y b 2
📊

Quick Reference

MethodDescriptionExample
reset_index()Moves multi-index levels to columnsdf.reset_index()
index.map()Combine multi-index levels into one stringdf.index.map(lambda x: f"{x[0]}_{x[1]}")
reset_index(drop=True)Remove index and do not keep as columnsdf.reset_index(drop=True)

Key Takeaways

Use df.reset_index() to flatten multi-index by turning index levels into columns.
Remember reset_index returns a new DataFrame unless inplace=True is set.
You can join multi-index levels into one string to create a flat index.
Avoid losing data by not using drop=True unless you want to remove index levels.
Flattening multi-index makes data easier to analyze and visualize.