Skip to main content
Albero Academy

Aggregation with agg

Compute several named summaries at once with agg.

Aggregation & Combining8 min

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

several summaries, named
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

a dict of column → functions
df.groupby("programme").agg({
"package": ["mean", "max"],
"age": "mean",
})

Key takeaways

  1. 01agg computes multiple summaries per group in one call.
  2. 02Named aggregation — out=("col", "func") — produces clean, flat column names.
  3. 03You can pass custom functions (lambdas) as aggregations.
  4. 04The dict form aggregates several columns with different functions at once.

Finished this chapter?

Mark it complete to track your progress through Pandas.