Summarizing Data with a Pivot Table
A pivot table is a data analysis tool that enables you to summarize, reorganize, and explore large datasets by aggregating data, rotating perspectives, filtering, sorting, and grouping information into meaningful categories.Key Benefits
- Simplify Complex Data — Break down large datasets into manageable summaries
- Identify Trends — Reveal patterns not immediately obvious in raw data
- Flexible Analysis — Quickly change layout and structure to answer different questions
- Efficiency — Automate data analysis for faster, regular reporting
Pandas Pivot Table Syntax
Practical Example: Analyzing Odd Lots in AAPL Trades
This example demonstrates analyzing AAPL trade data by:- Extracting trades between 9:30 AM and 4 PM
- Creating an
odd_lotsboolean column (shares < 100) - Extracting hour from timestamp
- Pivoting to count shares by hour and lot size
- Calculating percentage of odd lots per hour
Parameter Reference
| Parameter | Purpose |
|---|---|
aggfunc | Specifies aggregation function ('sum', 'mean', etc.) |
values | Target column for aggregation |
index | Column(s) defining row groupings |
columns | Column(s) defining column splits |
fill_value | Default value for missing data |

