Learn / Pandas
Intermediate

GroupBy & Aggregation

Split-apply-combine: pandas' most powerful single idea.

works in three steps you don't have to write yourself: split the data into groups by some key, apply a function to each group, then combine the results back together. .agg/.apply return one row per group (the data shrinks); .transform returns one row per original row (same shape as the input, values broadcast per group).

CallUse it forExample

.groupby(k).get_group(v)

pull out just one group

.groupby('animal').get_group('cat') → just the cat rows

.groupby(k).filter(fn)

keep whole groups matching a group-level condition

keep only groups where len(group) > 1

.pivot_table(values, index, columns, aggfunc)

spreadsheet-style cross-tab

rows=animal, columns=year, values=mean age

Now practice it

Exercises that use what this lesson just covered.

Missing Data & TransformationsMerging, Joining & Reshaping