How it works
Grouping splits rows by a key, applies an aggregation to each group, and combines the results into a new table.
Use grouping to answer per-category questions rather than looping over categories manually.
Common error
Reporting an average over groups of very different sizes without saying so, which hides the imbalance.
Worked example
These examples require NumPy or pandas, as shown by the imports.
Python
import pandas as pd
df = pd.DataFrame({"team": ["A", "B", "A"], "score": [4, 9, 8]})
means = df.groupby("team")["score"].mean()
for team in sorted(means.index):
print(team, float(means[team]))Output
A 6.0
B 9.0Grouping computes separate summaries rather than mixing categories. Include group sizes when they affect how confidently the results can be interpreted.
Exercises
6.4.1 · Explain
Why can averaging group averages give the wrong overall mean when group sizes differ?
Show solution
It gives equal weight to groups rather than observations. Use counts and sums, or an appropriate weighted mean, when calculating the overall observation mean.
6.4.2 · Write code
For groups A, A, B with values 2, 6, 5, print each group total in sorted order.
Show solution
Group by the category and sum the value column.
One valid solution
import pandas as pd
df = pd.DataFrame({"group": ["A", "A", "B"], "value": [2, 6, 5]})
totals = df.groupby("group")["value"].sum()
for group in sorted(totals.index):
print(group, int(totals[group]))Expected output
A 8
B 5