Skip to content
LearnPY
pandas

Group rows and summarize

Split a table into groups, aggregate each one, and use transform when the group total needs to travel back to every row.

11 min readbeginnerpandas

By the end

  • Explain the split-apply-combine model.
  • Aggregate one or more columns with groupby.
  • Name output columns with agg.
  • Use transform to create group-level values on every row.
  • Count rows without accidentally skipping missing values.

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

Ready
Loading editor…

Output

Press Run to execute this cell. The first run downloads Python; later runs reuse the session.

  1. Split: rows are assigned to a group by the region value.
  2. Apply: the sum of revenue is computed inside each group.
  3. 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

Ready
Loading editor…

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

Ready
Loading editor…

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

Ready
Loading editor…

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

Ready
Loading editor…

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

Ready
Loading editor…

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

Ready
Loading editor…

Results

Run the tests to check your solution. The first run downloads Python; later runs reuse the session.

Quiz

Check your understanding

01What are the three stages of a groupby operation?
02Which grouping result counts rows even when a value is missing?
03What shape does a groupby transform return?

Lesson progress