Examples
import numpy as np
import pandas as pd
df = pd.DataFrame({"P": ["f1", "f1", "f1", "f1", "f1",
"b1", "b1", "b1", "b1"],
"Q": ["one", "one", "one", "two", "two",
"one", "one", "two", "two"],
"R": ["small", "large", "large", "small",
"small", "large", "small", "small",
"large"],
"S": [1, 2, 2, 3, 3, 4, 5, 6, 7],
"T": [2, 4, 5, 5, 6, 6, 8, 9, 9]})
df
This first example aggregates values by taking the sum:
table = pd.pivot_table(df, values='S', index=['P', 'Q'],
columns=['R'], aggfunc=np.sum)
table
You can also fill missing values using the fill_value parameter:
table = pd.pivot_table(df, values='S', index=['P', 'Q'],
columns=['R'], aggfunc=np.sum, fill_value=0)
table
Following example aggregates by taking the mean across multiple columns.
table = pd.pivot_table(df, values=['S', 'T'], index=['P', 'R'],
aggfunc={'S': np.mean,
'T': np.mean})
table
You can also calculate multiple types of aggregations for any
given value column:
table = pd.pivot_table(df, values=['S', 'T'], index=['P', 'R'], aggfunc={'S': np.mean, 'T': [min, max, np.mean]}) table