Split, apply, combine
Grouping answers questions such as "what was revenue by region?" The grouping column defines the groups. pandas splits the rows by that value, applies an aggregation to each group, and combines the one-row-per-group results into a new Series or DataFrame.
Python
One total per region
Output
Press Run to execute this cell. The first run downloads Python; later runs reuse the session.
- Split: rows are assigned to a group by the region value.
- Apply: the sum of revenue is computed inside each group.
- Combine: the per-group totals are assembled into a Series indexed by region.
Name your aggregates
A single aggregation can return a Series, but an analysis usually needs several columns at once. The named aggregation form gives each output a clear name and avoids multi-level column labels.
Python
Named output columns
Output
Press Run to execute this cell. The first run downloads Python; later runs reuse the session.
Set as_index=False when you want the grouping key to remain an ordinary column. With the default as_index=True, region becomes the index of the summary instead.
Group by more than one key
Passing a list of column names groups by the combination of those values. Each key becomes a level of a MultiIndex, so the resulting rows represent region-product pairs rather than regions alone.
Python
Two keys produce a MultiIndex
Output
Press Run to execute this cell. The first run downloads Python; later runs reuse the session.
Use reset_index when a downstream step expects ordinary columns, or keep the MultiIndex when you want the hierarchy to remain visible in the result.
Use transform for group-level values
Aggregation returns one row per group. transform returns one value per original row, which is what you need when a group total should sit beside each row for comparison or for a share-of-total calculation.
Python
A group total repeated on every row
Output
Press Run to execute this cell. The first run downloads Python; later runs reuse the session.
Count rows, not just values
count counts non-missing values in each group. size counts rows, including rows with missing values. If a column can contain NaN, the two answers can differ, and the difference is often the information you need.
Python
Rows versus non-missing prices
Output
Press Run to execute this cell. The first run downloads Python; later runs reuse the session.
The North group has two rows but only one observed price. Use size for row counts and count for observed values, then be explicit about which one the question is asking for.
A grouping recipe
For most reports: choose the grouping keys, choose the measurements, name the aggregations, and sort the final summary. Keeping those decisions separate makes the result easy to check against the raw rows.
Python
Group, name, and order
Output
Press Run to execute this cell. The first run downloads Python; later runs reuse the session.
The next lesson joins rows from different tables. Grouping combines rows that already share a key; merging brings in columns that live in another frame.
Practice
Make it stick
Run the tests as often as you like. Hints and the solution are available when you need them.
Exercise
Group sales by region and create summary with the columns total_units and total_revenue. Sort the rows by total_revenue from highest to lowest.
Your solution
ReadyResults
Run the tests to check your solution. The first run downloads Python; later runs reuse the session.
Quiz
Check your understanding
Lesson progress