6 · GroupBy (“split‑apply‑combine”)

6.1 Basic Grouping

# group by a single column
g = df.groupby("segment")

Iterate or inspect groups:

```

6.2 Aggregation

# single aggregation
```

agg1 = g\["age"].mean()

# multiple aggregations

agg2 = g.agg({
"age": \["mean", "min", "max"],
"salary": "sum"
}) 
```

6.3 Named Aggregations

# clean output with new column names
```

g2 = df.groupby("segment").agg(
avg\_age=("age", "mean"),
total\_salary=("salary", "sum"),
count=("age", "size")
) 
```

6.4 Transformation vs. Filter vs. Apply

```

6.5 Grouping on Multiple Keys & Index Levels

# multi‑key grouping
```

multi = df.groupby(\["department", "role"]).agg(
mean\_perf=("performance", "mean"),
n=("id", "size")
)

# group by index level

ts = df.set\_index(\["date", "category"])
res = ts.groupby(level="category").sum()
```

6.6 Time‑Aware Grouping

# group by month from datetime index
```

ts = df.set\_index("timestamp")
monthly = ts\["value"].groupby(pd.Grouper(freq="M")).sum()
```

6.7 Performance Tips

```