# group by a single column
g = df.groupby("segment")
Iterate or inspect groups:
for name, group in g: ...
g.get_group("young")
g.size()
⇒ counts per group# single aggregation
```
agg1 = g\["age"].mean()
# multiple aggregations
agg2 = g.agg({
"age": \["mean", "min", "max"],
"salary": "sum"
})
```
# clean output with new column names
```
g2 = df.groupby("segment").agg(
avg\_age=("age", "mean"),
total\_salary=("salary", "sum"),
count=("age", "size")
)
```
# z‑score within group
```
df\["age\_z"] = g\["age"].transform(lambda x: (x - x.mean()) / x.std())
# keep groups with >10 rows
df\_filt = g.filter(lambda x: len(x) > 10)
def top\_n(x, n=3):
return x.nlargest(n, "salary")
df\_top = g.apply(top\_n)
# 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()
```
# group by month from datetime index
```
ts = df.set\_index("timestamp")
monthly = ts\["value"].groupby(pd.Grouper(freq="M")).sum()
```
dict
then pd.concat
.