Real reports need more than one number per group. agg computes several summaries at once and lets you name the output columns — the difference between a quick stat and a presentable table.
Named aggregations
df.groupby("programme").agg(students=("name", "count"),avg_pkg=("package", "mean"),max_pkg=("package", "max"),spread=("package", lambda s: s.max() - s.min()),).reset_index()
Multiple columns & functions
df.groupby("programme").agg({"package": ["mean", "max"],"age": "mean",})
Key takeaways
- 01agg computes multiple summaries per group in one call.
- 02Named aggregation — out=("col", "func") — produces clean, flat column names.
- 03You can pass custom functions (lambdas) as aggregations.
- 04The dict form aggregates several columns with different functions at once.
