How can I remove the MultiIndex in a pivot table using Pandas?

How can I remove the MultiIndex in a pivot table using Pandas?

Pandas is a widely used Python library for data analysis and manipulation. It offers various functionalities for working with data, including the creation of pivot tables. A pivot table is a powerful tool for summarizing and analyzing data, but it can sometimes have a MultiIndex format, which can make it difficult to work with. To remove the MultiIndex in a pivot table using Pandas, one can use the `reset_index()` function. This function will convert the MultiIndex into a regular index, making the pivot table more manageable and easier to work with.

Pandas: Remove MultiIndex in Pivot Table


To remove a multiIndex from a pandas pivot table, you can use the values argument along with the reset_index() function:

pd.pivot_table(df, index='col1', columns='col2', values='col3').reset_index()

The following example shows how to use this syntax in practice.

Example: Remove MultiIndex in Pandas Pivot Table

Suppose we have the following pandas DataFrame that contains information about various basketball players:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'],
                   'position': ['G', 'G', 'F', 'F', 'G', 'F', 'F', 'F'],
                   'points': [4, 4, 6, 8, 9, 5, 5, 12]})

#view DataFrame
print(df)

  team position  points
0    A        G       4
1    A        G       4
2    A        F       6
3    A        F       8
4    B        G       9
5    B        F       5
6    B        F       5
7    B        F      12

Now suppose we create the following pivot table to summarize the mean value of points by team and position:

#create pivot table to summarize mean points by team and position
pd.pivot_table(df, index='team', columns='position')

	        points
position	F    G
team		
A	7.000000   4.0
B	7.333333   9.0

The resulting pivot table summarizes the mean value of points by team and position, but contains a multiIndex.

To remove the multiIndex, we can use the values argument within the pivot_table() function and add reset_index() to the end:

#create pivot table to summarize mean points by team and position
pd.pivot_table(df, index='team', columns='position', values='points').reset_index()

position  team	F	  G
0	  A	7.000000  4.0
1	  B	7.333333  9.0

The resulting pivot table summarizes the mean value of points by team and position and no longer has a multiIndex.

Note that the pivot_table() function calculates the mean value by default.

To calculate a different metric, such as the sum, use the aggfunc argument as follows:

#create pivot table to summarize sum of points by team and position
pd.pivot_table(df, index='team', columns='position', values='points',
               aggfunc='sum').reset_index()

position  team	F   G
0	  A	14  8
1	  B	22  9

The resulting pivot table summarizes the sum of values of points by team and position and also has no multiIndex.

Note: You can find the complete documentation for the pandas pivot_table() function .

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

Cite this article

stats writer (2024). How can I remove the MultiIndex in a pivot table using Pandas?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-remove-the-multiindex-in-a-pivot-table-using-pandas/

stats writer. "How can I remove the MultiIndex in a pivot table using Pandas?." PSYCHOLOGICAL SCALES, 26 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-remove-the-multiindex-in-a-pivot-table-using-pandas/.

stats writer. "How can I remove the MultiIndex in a pivot table using Pandas?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-remove-the-multiindex-in-a-pivot-table-using-pandas/.

stats writer (2024) 'How can I remove the MultiIndex in a pivot table using Pandas?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-remove-the-multiindex-in-a-pivot-table-using-pandas/.

[1] stats writer, "How can I remove the MultiIndex in a pivot table using Pandas?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I remove the MultiIndex in a pivot table using Pandas?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top