
This morning you learned a new code pattern - split-apply-combine - that answers most grouped questions in one line. This afternoon we will look at the questions it does not answer, and at the loop, which is the older and slower tool that handles them.
.groupby() gives you back one number per group, which covers a great deal of what you want. It does not cover printing a paragraph about each site, saving a separate file for each species, running a check that either passes or fails per group, or doing anything at all where the answer is not a number.
For any of those jobs, you take the groups apart and walk through them with a for loop. You met for on Tuesday afternoon over a list. Today the thing you loop over is a set of DataFrames.
We will also spend about ten minutes reading code you are not expected to write. List comprehensions are everywhere in real Python, including in code you may inherit from a colleague later on, and you should be able to look at one and say what it does.
How a live coding session works
We all start from an empty notebook and write everything together, live. There is no code on this page to copy, on purpose.
- Cella or Kelly writes a line, explains it, and runs it. You type the same line and run it.
- When something breaks, we fix it together. Things will break, and fixing them is the useful part.
- If you fall behind, say so! Cella and Kelly would much rather slow down than lose you.
- Type the code. Do not copy it from a neighborβs screen. The typing is the point.
Set up your notebook
Create a new notebook named
Session_5D_Loops_Over_Groups.ipynb.Add a title cell:
# Day 5: Session 5D - When a Pattern Is Not Enough
Date: 09/04/2026- Add the following markdown cells now, in this order, leaving space under each. We will fill in the code as we go, and having the structure in place means your notes stay organized instead of becoming one long scroll.
## 1. What groupby is holding
## 2. Looping over the groups
## 3. One group is a DataFrame
## 4. Building something up as you go
## 5. Reading a list comprehension
## 6. Comprehensions you will meet in the wild
## 7. Which one should I reach for?- Under each heading, add an empty code cell.
Your seven headings are the scaffold for the session. Read the next section before you switch to your notebook, so you know what the afternoon covers.
What we will cover
Looping over groups (about 15 minutes)
- what a
groupbyobject is actually holding, and how to look inside it - the two-variable
forloop:for name, group in grouped: - why one group is a full DataFrame, and what that lets you do
- printing a formatted report, one line per group
- collecting results into a list or a dictionary as you go
Reading comprehensions (about 10 minutes)
- a
forloop that builds a list, written the long way - the same loop as a list comprehension
- a dictionary comprehension
- a comprehension with an
ifin it - predicting the output of each before we run it
Choosing (about 5 minutes)
- the three questions that tell you whether you need a loop at all
- why
.groupby(...).mean()is almost always the right answer, and when a loop is the better one
What you should be able to do afterwards
- explain what
.groupby()is holding before you call an aggregation on it - write a
for name, group in grouped:loop and explain what each of the two variables holds - produce something for each group other than a summary number
- read a list comprehension and predict what it produces
- decide, for a given task, whether it wants the grouped pattern or a loop
Sections 5 and 6 are a reading exercise. Nothing this week, including the end-of-day practice and next weekβs project, requires you to write a comprehension. We read a few today so that meeting one in somebody elseβs notebook later is not the first time you have seen the syntax.