agg() (Pandas)
agg() (aggregate) applies one or more reducing functions to a DataFrame, Series, or GroupBy and collapses each column or group to a single summary value. It lets you compute several statistics in a single call.
Pandas
Pass a single function name like 'sum', a list like ['min', 'max', 'mean'] to get several stats at once, or a dict mapping each column to its own functions for fine-grained control. The result is a tidy frame of the summaries.
agg() shines on a GroupBy: df.groupby('team').agg({'points': 'mean', 'wins': 'sum'}) produces one row per team with different aggregations per column. Named aggregation — agg(avg=('points','mean')) — even lets you name the output columns directly.
Any function that reduces an array to one value works, including your own. It is distinct from transform(), which returns a result the same length as the input rather than a single summary.
Syntax
df.agg(func) | gb.agg({'col': 'mean', 'col2': ['min','max']})Examples
Python
import pandas as pd
df = pd.DataFrame({"v": [1, 2, 3, 4]})
print(df.agg(["min", "max", "mean"]))Output:
v
min 1.0
max 4.0
mean 2.5