Table of Contents
The process of flattening a MultiIndex in Pandas refers to restructuring a dataframe so that all levels of the index are merged into a single level. This allows for easier data manipulation and analysis. To flatten a MultiIndex, the reset_index() function can be used. This will convert the index to a regular column and create a new numerical index. Examples of flattening a MultiIndex in Pandas can include using the reset_index() function on a dataframe with multiple levels of index, such as a sales data table with product categories and regions as the index levels. This will create a single level index with all the relevant information merged together. Additionally, the reset_index() function can also be used on a pivot table with multiple columns, resulting in a flattened table with all the data in a single row.
Flatten MultiIndex in Pandas (With Examples)
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 DataFramedf 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 35And 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 35Cite this article
stats writer (2024). How can I flatten a MultiIndex in Pandas and provide examples?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-flatten-a-multiindex-in-pandas-and-provide-examples/
stats writer. "How can I flatten a MultiIndex in Pandas and provide examples?." PSYCHOLOGICAL SCALES, 6 May. 2024, https://scales.arabpsychology.com/stats/how-can-i-flatten-a-multiindex-in-pandas-and-provide-examples/.
stats writer. "How can I flatten a MultiIndex in Pandas and provide examples?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-flatten-a-multiindex-in-pandas-and-provide-examples/.
stats writer (2024) 'How can I flatten a MultiIndex in Pandas and provide examples?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-flatten-a-multiindex-in-pandas-and-provide-examples/.
[1] stats writer, "How can I flatten a MultiIndex in Pandas and provide examples?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.
stats writer. How can I flatten a MultiIndex in Pandas and provide examples?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
