df.pivot_table(index="region",
columns="quarter",
values="amount",
aggfunc="sum")
Rows = region. Columns = quarter. Cells = total amount.
df.pivot_table(index="region",
columns="quarter",
values="amount",
aggfunc=["sum", "mean", "count"])
df.pivot_table(index="region",
values=["amount", "quantity"],
aggfunc={"amount": "sum", "quantity": "sum"})
df.pivot_table(index="region", columns="quarter",
values="amount", aggfunc="sum", fill_value=0)
df.pivot_table(index="region", columns="quarter",
values="amount", aggfunc="sum",
margins=True, margins_name="Total")
pd.crosstab(df["region"], df["status"])
pd.crosstab(df["region"], df["status"], normalize="index") # row percentages
pivot_table(index, columns, values, aggfunc) = Excel pivot.fill_value=0 stops NaN holes.margins=True adds totals.pd.crosstab is the shortcut for frequency tables.Build a pivot of total sales with products as rows and months as columns. Add a totals row and column.