Pivot Tables and Cross-Tabulation
pd.pivot_table(), pd.crosstab(), reshaping data for summary statistics.
Reshaping for Insight
A pivot table reshapes long data into a grid, summarizing one value across two categorical axes. It is the spreadsheet pivot you know, but programmable.
import pandas as pd
df = pd.DataFrame({
"region": ["E", "E", "W", "W", "E"],
"product": ["A", "B", "A", "B", "A"],
"sales": [10, 20, 30, 40, 50],
})Basic pivot_table
pd.pivot_table takes values (the numbers to summarize), index (rows), and columns (columns). By default it averages duplicate cells.
pt = pd.pivot_table(df, values="sales", index="region", columns="product")
print(pt)
# product A B
# region
# E 30.0 20.0
# W 30.0 40.0All lessons in this course
- GroupBy and Aggregation
- Pivot Tables and Cross-Tabulation
- Advanced Merging and Joining
- Time Series in Pandas