Create a pandas Pivot Table
Pivot tables turn row-level data into a compact report organized by categories and aggregate values.
What is Create a pandas Pivot Table?
Pivot tables turn row-level data into a compact report organized by categories and aggregate values.
Summarize grouped values with a pandas pivot table.
When should you use it?
- Read, clean, and transform tabular data.
- Analyze data from CSV, Excel, APIs, or databases.
- Prepare reports, statistics, and chart data.
Example code
main.py
import pandas as pd
results = pd.DataFrame({
"team": ["A", "A", "B", "B"],
"score": [80, 90, 70, 100],
})
print(results.pivot_table(index="team", values="score", aggfunc="mean"))
Expected output
score
team
A 85.0
B 85.0
How it works
The index defines report rows, values selects the numeric column, and mean calculates the aggregate for each team.
Change the values and run the program in the CodeUtility online Python compiler without installing Python locally.
Practice exercises
Modify the runnable example with the exercises below to build understanding beyond copying the result.
- Add rows, columns, and missing values.
- Combine filtering, sorting, and groupby into a small report.
- Load a real file, inspect dtypes, and handle invalid data.