How can I add subtotals to a pivot table in Pandas?

How can I add subtotals to a pivot table in Pandas?

Adding subtotals to a pivot table in Pandas is a simple process that allows for better organization and analysis of data. By using the “aggfunc” parameter in the pivot_table function, one can specify the type of subtotals they want to include, such as sum, mean, or count. The “margins” parameter can also be used to add a row and column for overall totals. This feature is useful for summarizing and comparing data in a pivot table, making it easier to understand and draw insights from the data set.

Pandas: Add Subtotals to Pivot Table


Often you may want to add subtotals to a pandas pivot table.

Fortunately this is easy to do using built-in functions in pandas.

The following example shows how to do so.

Example: Add Subtotals to 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'],
                   'all_star': ['Y', 'N', 'Y', 'Y', 'N', 'N', 'N', 'Y'],
                   'points': [4, 4, 6, 8, 9, 5, 5, 12]})

#view DataFrame
print(df)

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

We can use the following code to create a pivot table in pandas that shows the sum of points for each combination of team, all_star, and position in the DataFrame:

#create pivot table
my_table = pd.pivot_table(df, values='points',
                              index=['team', 'all_star'],
                              columns='position',
                              aggfunc='sum')

#view pivot table
print(my_table)

position          F    G
team all_star           
A    N          NaN  4.0
     Y         14.0  4.0
B    N         10.0  9.0
     Y         12.0  NaN

Now suppose we would like to add a subtotals row that shows the subtotal of points for each team and position.

We can use the following syntax to do so:

#add subtotals row to pivot table
pd.concat([
    y.append(y.sum().rename((x, 'Total')))
    for x, y in my_table.groupby(level=0)
]).append(my_table.sum().rename(('Grand', 'Total')))

	position	F	G
team	all_star		
A	       N	NaN	4.0
               Y	7.0	4.0
           Total	7.0	8.0
B	       N	5.0	9.0
               Y	12.0	NaN
           Total	17.0	9.0
Grand	   Total	24.0	17.0

We now have two subtotal rows that show the subtotal of points for each team and position, along with a grand total row that shows the grand total of each column.

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

Additional Resources

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

Cite this article

stats writer (2024). How can I add subtotals to a pivot table in Pandas?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-add-subtotals-to-a-pivot-table-in-pandas/

stats writer. "How can I add subtotals to a pivot table in Pandas?." PSYCHOLOGICAL SCALES, 27 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-add-subtotals-to-a-pivot-table-in-pandas/.

stats writer. "How can I add subtotals to a pivot table in Pandas?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-add-subtotals-to-a-pivot-table-in-pandas/.

stats writer (2024) 'How can I add subtotals to a pivot table in Pandas?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-add-subtotals-to-a-pivot-table-in-pandas/.

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

stats writer. How can I add subtotals to a pivot table in Pandas?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

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