Table of Contents
A pivot table in Pandas is a powerful tool for summarizing and analyzing data. It allows users to easily group and aggregate data based on different criteria. To create a pivot table that displays the sum of values, follow these steps:
1. Load your data into a Pandas DataFrame.
2. Use the `pivot_table()` function, specifying the column(s) you want to group by as the `index` parameter and the column with the values you want to sum as the `values` parameter.
3. Set the `aggfunc` parameter to `’sum’` to calculate the sum of values for each group.
4. Optional: You can also specify additional columns to display as columns in the pivot table by setting the `columns` parameter.
5. Run the code and the resulting pivot table will display the sum of values for each group.
By utilizing the pivot table function in Pandas, users can easily and efficiently summarize their data by calculating the sum of values for different groups, providing valuable insights for data analysis and decision making.
Pandas: Create Pivot Table with Sum of Values
You can use the following basic syntax to create a pivot table in pandas that displays the sum of values in certain columns:
pd.pivot_table(df, values='col1', index='col2', columns='col3', aggfunc='sum')
The following example shows how to use this syntax in practice.
Example: Create Pandas Pivot Table With Sum of Values
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
The following code shows how to create a pivot table in pandas that shows the sum of ‘points’ values for each ‘team’ and ‘position’ in the DataFrame:
#create pivot table
df_pivot = pd.pivot_table(df, values='points', index='team', columns='position',
aggfunc='sum')
#view pivot table
print(df_pivot)
position F G
team
A 14 8
B 22 9From the output we can see:
- Players on team A in position F scored a total of 14 points.
- Players on team A in position G scored a total of 8 points.
- Players on team B in position F scored a total of 22 points.
- Players on team B in position G scored a total of 9 points.
Note that we can also use the margins argument to display the margin sums in the pivot table:
#create pivot table with margins
df_pivot = pd.pivot_table(df, values='points', index='team', columns='position',
aggfunc='sum', margins=True, margins_name='Sum')
#view pivot table
print(df_pivot)
position F G Sum
team
A 14 8 22
B 22 9 31
Sum 36 17 53The pivot table now displays the row sums and column sums.
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 create a pivot table in Pandas that displays the sum of values?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-create-a-pivot-table-in-pandas-that-displays-the-sum-of-values/
stats writer. "How can I create a pivot table in Pandas that displays the sum of values?." PSYCHOLOGICAL SCALES, 27 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-create-a-pivot-table-in-pandas-that-displays-the-sum-of-values/.
stats writer. "How can I create a pivot table in Pandas that displays the sum of values?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-create-a-pivot-table-in-pandas-that-displays-the-sum-of-values/.
stats writer (2024) 'How can I create a pivot table in Pandas that displays the sum of values?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-create-a-pivot-table-in-pandas-that-displays-the-sum-of-values/.
[1] stats writer, "How can I create a pivot table in Pandas that displays the sum of values?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I create a pivot table in Pandas that displays the sum of values?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
