You can calculate different statistics in a pivot table by passing a different aggregation function to the aggfunc parameter in the pivot_table() method. Here's an example:
import pandas as pd# create a pandas DataFramedf = pd.DataFrame({'Region': ['North', 'North', 'South', 'South', 'West', 'West'], 'Salesperson': ['Alice', 'Bob', 'Charlie', 'Dave', 'Eve', 'Frank'], 'Sales': [100, 200, 150, 50, 75, 125], 'Profit': [20, 50, 30, 10, 15, 25]})# create a pivot table that summarizes the sales and profit data by region and salespersonpivot = pd.pivot_table(df, values=['Sales', 'Profit'], index=['Region'], columns=['Salesperson'], aggfunc={'Sales': sum, 'Profit': 'mean'})print(pivot) |
This will create a pivot table that summarizes the sales and profit data by region and salesperson. The resulting output will be:
Profit Sales Salesperson Alice Bob Charlie Dave Eve Frank Alice Bob Charlie Dave Eve FrankRegion North 20 50.0 NaN NaN NaN NaN 100 200 NaN NaN NaN NaNSouth NaN NaN 30.0 10.0 NaN NaN NaN NaN 150.0 50.0 NaN NaNWest NaN NaN NaN NaN 15.0 25.0 NaN NaN NaN NaN 75.0 125.0 |
The resulting pivot table shows the mean profit for each salesperson broken down by region, as well as the total sales for each salesperson broken down by region. The aggfunc parameter is passed a dictionary that specifies the aggregation functions to use for each column. In this case, we're using the sum function to summarize the sales data, and the 'mean' string to summarize the profit data. You can pass any function that can be used to summarize data, including built-in functions such as sum(), mean(), max(), min(), and custom functions.