pandas in depth · lesson 1 of 12
Parts in this lesson
Adf.groupby("store")["kilos"].sum()one answer per store
Bagg(total=("kilos", "sum"))several answers, named
Cs.str.strip()remove spaces at either end

Grouping: split, apply, combine

Last updated

Most questions about sales are questions about groups: how much did each store sell, which item does best in each town, what's the average day per store. pandas answers all of them with one idea, split-apply-combine, and one method, groupby. In this lesson you'll learn both, and the stray space that can quietly split one store into two.

By the end of this lesson you can

  • Explain split-apply-combine, and do it with groupby
  • Work out several summaries at once with agg, with names you choose
  • Group by more than one column
  • Say the difference between size and count

Split, apply, combine

Stage 4 uses a slightly bigger sales file: two days, three stores, and two items, rice and sugar. Twelve rows. Step through how pandas answers "how many kilos did each store sell?":

Split, apply, combine. The sales table: twelve rows, three stores mixed together. The question: how many kilos did each store sell?Batangas42Batangas12Lipa35Lipa10Tanauan29Tanauan8Batangas38Batangas11Lipa33Lipa9Tanauan30Tanauan7salesstorekilos
1 of 4 The sales table: twelve rows, three stores mixed together. The question: how many kilos did each store sell?

Hadley Wickham named the idea split-apply-combine in 2011: split the rows into groups, apply a summary to each group on its own, and combine the answers into one table. In pandas it's one line:

sales.groupby("store")["kilos"].sum()

Read it left to right: group the sales by store, take the kilos column, add it up within each group.

Several answers at once

Run the cells.

groups.ipynb
  • .agg(["sum", "mean", "max"]) applies several summaries, one column each.
  • Named aggregation, the third cell, lets you choose each output's name and its column: total=("kilos", "sum") means "a column called total, the sum of kilos". days=("date", "nunique") counts the different dates. This is the form to use in real work, because the result's column names say what they are.
  • Several keys: groupby(["store", "item"]) makes one group for each store and item pair. Batangas sold 80 kg of rice and 23 of sugar. The answer has a two-level index; lesson 4.5 shows how to use it.

The store names became the index of the answer. To have them back as an ordinary column, add as_index=False to groupby, or .reset_index() at the end.

size and count

Two ways to count rows in a group, which differ when something's missing. Run the cells.

counts.ipynb
  • size() counts the rows in each group: Lipa has 3.
  • count() counts the values that aren't missing: Lipa has 2, because one visit has no kilos.

Use size for "how many records", and count for "how many records have this value". The gap between them is how many are missing in each group, which is often worth knowing.

Wrong way: Batangas and Batangas

Run the first cell.

spaces.ipynb

Batangas appears twice: 82 kg and 38 kg. One row was typed as "Batangas ", with a space at the end. You can't see it in the output, but to pandas it's a different name, so it's a different group. The store's real total, 120 kg, is split in two, and a chart of "sales by store" would show four stores.

The second cell cleans the column with str.strip(), which removes spaces from both ends, and the groups come out right. Spaces, capitals (LIPA) and spellings (Lipa City) all split groups the same way. Before you group by a text column, look at value_counts() (lesson 3.1) and fix what it shows; lesson 4.8 has the tools.

Check what you learned

What does sales.groupby("item")["kilos"].mean() give?
A group has 10 rows, and 3 of them have no price. What are size() and ["price"].count()?
Grouping by store shows both Lipa and Lipa . What’s the fix?

Sources

Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.

All sources for Simple Data Analysis