pivot_table() function
import numpy as np
import pandas as pd
df = pd.DataFrame({"P": ["s1", "s1", "s1",
"b1", "b1", "b1", "b1"],
"Q": ["one", "one", "one",
"one", "one", "two", "two"],
"R": ["small", "large", "large", "small",
"small", "large", "small"],
"S": [2, 2, 3, 3, 4, 5, 6],
"T": [4, 5, 5, 7, 8, 8, 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 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 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