0
0
Pandasdata~10 mins

Iterating over groups in Pandas - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Iterating over groups
Start with DataFrame
Group data by column
For each group in groups
Access group name and data
Process or print group
Repeat for next group
End
We start with a DataFrame, group it by a column, then loop over each group to access its name and data for processing.
Execution Sample
Pandas
import pandas as pd

df = pd.DataFrame({'Team': ['A', 'A', 'B', 'B'], 'Points': [10, 15, 10, 20]})
for name, group in df.groupby('Team'):
    print(name)
    print(group)
This code groups the DataFrame by 'Team' and prints each group name and its rows.
Execution Table
StepActionGroup NameGroup Data (subset of DataFrame)
1Group DataFrame by 'Team'
2Start iteration over groups
3First group accessedA{'Team': ['A', 'A'], 'Points': [10, 15]}
4Print group name and dataA{'Team': ['A', 'A'], 'Points': [10, 15]}
5Second group accessedB{'Team': ['B', 'B'], 'Points': [10, 20]}
6Print group name and dataB{'Team': ['B', 'B'], 'Points': [10, 20]}
7No more groups
💡 All groups have been iterated over and printed.
Variable Tracker
VariableStartAfter 1After 2Final
nameundefinedABB
groupundefined{'Team': ['A', 'A'], 'Points': [10, 15]}{'Team': ['B', 'B'], 'Points': [10, 20]}{'Team': ['B', 'B'], 'Points': [10, 20]}
Key Moments - 2 Insights
Why does the loop give both a group name and a group DataFrame?
Because df.groupby('Team') returns pairs: the group key (name) and the subset DataFrame (group). See execution_table rows 3 and 5.
What happens if the DataFrame has many groups?
The loop repeats for each group, giving you each group's name and data one by one, as shown in execution_table rows 3-6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the group name?
AA
BB
CPoints
DTeam
💡 Hint
Check the 'Group Name' column at step 3 in execution_table.
At which step does the loop finish iterating over all groups?
AStep 5
BStep 6
CStep 7
DStep 4
💡 Hint
Look at the exit_note and the last step in execution_table.
If the DataFrame had a third group 'C', how would variable 'name' change after the third iteration?
AIt would be 'B'
BIt would be 'C'
CIt would be undefined
DIt would be 'A'
💡 Hint
See variable_tracker for how 'name' updates each iteration.
Concept Snapshot
Use df.groupby('column') to split data into groups.
Loop with: for name, group in df.groupby('column'):
  'name' is the group key.
  'group' is the DataFrame subset.
Process each group inside the loop.
Useful for separate analysis per group.
Full Transcript
We start with a DataFrame and group it by a column using pandas groupby. This creates groups based on unique values in that column. Then, we loop over these groups. Each loop gives us the group name (the unique value) and the group data (a smaller DataFrame with only rows for that group). We can print or analyze each group separately. The loop continues until all groups are processed. Variables 'name' and 'group' update each iteration to hold the current group's info. This method helps to handle data in chunks based on categories.