Lesson 9: Grouping, Aggregation & Pivot Tables

1. Grouping & Aggregation

Group by category → compute stats per group (like Excel pivot table).

df.groupby("species")["petal_length"].mean()
# Average petal length per flower species

Multiple aggregations:

df.groupby("city").agg({
    "age": "mean",
    "salary": ["min", "max", "mean"]
})

Exercise 1

To get average salary per department:
df.groupby("")[""].()

2. Pivot Tables

Reshape data — summarize with rows/columns.

pd.pivot_table(df, 
    values="salary",
    index="department",
    columns="year",
    aggfunc="mean")

Used in ML: compare features across categories.

Exercise 2

What does groupby + mean() return?

Exercise 3

Which aggfunc options are valid in groupby.agg()?
← Previous Lesson (8) Next Lesson (10) →