4.3 C
London
Wednesday, March 12, 2025
HomePandas in PythonDataFrame Functions in PythonHow to Flatten MultiIndex in Pandas (With Examples)

How to Flatten MultiIndex in Pandas (With Examples)

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

You can use the following basic syntax to flatten a MultiIndex in pandas:

#flatten all levels of MultiIndex
df.reset_index(inplace=True)

#flatten specific levels of MultiIndex
df.reset_index(inplace=True, level = ['level_name'])

The following examples show how to use this syntax in practice.

Example 1: Flatten All Levels of MultiIndex in Pandas

Suppose we have the following MultiIndex pandas DataFrame:

import pandas as pd

#create DataFrame
index_names = pd.MultiIndex.from_tuples([('Level1','Lev1', 'L1'),
                                       ('Level2','Lev2', 'L2'),
                                       ('Level3','Lev3', 'L3'),
                                       ('Level4','Lev4', 'L4')],
                                       names=['Full','Partial', 'ID'])

data = {'Store': ['A','B','C','D'],
        'Sales': [12, 44, 29, 35]}

df = pd.DataFrame(data, columns = ['Store','Sales'], index=index_names)

#view DataFrame
df

			Store	Sales
Full	Partial	ID		
Level1	Lev1	L1	A	17
Level2	Lev2	L2	B	22
Level3	Lev3	L3	C	29
Level4	Lev4	L4	D	35

We can use the following syntax to flatten every level of the MultiIndex into columns in the DataFrame:

#flatten every level of MultiIndex 
df.reset_index(inplace=True)

#view updated DataFrame
df

        Full	Partial	ID	Store	Sales
0	Level1	Lev1	L1	A	12
1	Level2	Lev2	L2	B	44
2	Level3	Lev3	L3	C	29
3	Level4	Lev4	L4	D	35

Notice that each level of the MultiIndex is now a column in the DataFrame.

Example 2: Flatten Specific Levels of MultiIndex in Pandas

Suppose we have the same pandas DataFrame as the previous example:

#view DataFrame
df

			Store	Sales
Full	Partial	ID		
Level1	Lev1	L1	A	12
Level2	Lev2	L2	B	44
Level3	Lev3	L3	C	29
Level4	Lev4	L4	D	35

The following code shows how to flatten just one specific level of the MultiIndex:

#flatten 'ID' level only
df.reset_index(inplace=True, level = ['ID'])

#view updated DataFrame
df

		ID	Store	Sales
Full	Partial			
Level1	Lev1	L1	A	12
Level2	Lev2	L2	B	44
Level3	Lev3	L3	C	29
Level4	Lev4	L4	D	35

And the following code shows how to flatten several specific levels of the MultiIndex:

#flatten 'ID' level only
df.reset_index(inplace=True, level = ['Partial', 'ID'])

#view updated DataFrame
df

	Partial	 ID	Store	Sales
Full				
Level1	Lev1	 L1	A	12
Level2	Lev2	 L2	B	44
Level3	Lev3	 L3	C	29
Level4	Lev4	 L4	D	35

Additional Resources

The following tutorials explain how to perform other common functions in pandas:

How to Convert Index to Column in Pandas
How to Rename Index in Pandas
How to Set Column as Index in Pandas

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories