You can calculate multiple statistics in a pivot table by passing a list of aggregation functions 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=[sum, '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:
sum mean Profit Sales Profit Sales Salesperson Alice Bob Charlie Dave Eve Frank 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 NaN 20.0 50.0 NaN NaN NaN NaNSouth NaN NaN 30.0 10.0 NaN NaN NaN NaN 150.0 50.0 NaN NaN NaN NaN 150.0 50.0 NaN NaNWest NaN NaN NaN NaN 15 25.0 NaN NaN NaN NaN 75 125.0 NaN NaN NaN NaN 75 125.0 |
The resulting pivot table shows the total sales and mean profit for each salesperson broken down by region. The aggfunc parameter is passed a list of functions that specifies the aggregation functions to use for each column. In this case, we're using the sum and 'mean' functions to summarize the 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.