
This morning you learned a sentence that answers most grouped questions in one line. This afternoon we look at the questions it does not answer, and at the older, slower tool that does.
.groupby() gives you back one number per group. That 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 those, 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 the last ten minutes reading code you are not expected to write. List comprehensions are everywhere in real Python, including in code you will inherit from a colleague on your first day of work, and you should be able to look at one and say what it does.
How a live coding session works
We both start from an empty notebook and write everything together, live. There is no code on this page to copy, on purpose.
- I write a line, explain it, and run it. You write the same line and run it.
- When something breaks, we fix it together. Things will break. That is the useful part.
- If you fall behind, say so. Someone else is behind too, and I 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 Sentence 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.
That is your scaffold. Now close this page and switch to your notebook.
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 it is not
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 say what each of the two variables is - do something per group that is not a summary number
- read a list comprehension and predict what it produces
- say, for a given task, whether it wants the sentence or the loop
Sections 5 and 6 are a reading exercise. Nothing this week, including tonightβs practice and next weekβs project, requires you to write a comprehension. They are here so that the first time you meet one in somebody elseβs notebook is not the first time you meet one.