polars.internals.frame.GroupBy.agg

GroupBy.agg(column_to_agg: Union[List[Tuple[str, List[str]]], Dict[str, Union[str, List[str]]], List[polars.internals.expr.Expr], polars.internals.expr.Expr]) polars.internals.frame.DF

Use multiple aggregations on columns. This can be combined with complete lazy API and is considered idiomatic polars.

Parameters
column_to_agg

map column to aggregation functions.

Returns
Result of groupby split apply operations.

Examples

>>> df = pl.DataFrame(
...     {"foo": ["one", "two", "two", "one", "two"], "bar": [5, 3, 2, 4, 1]}
... )
>>> df.groupby("foo").agg(
...     [
...         pl.sum("bar").suffix("_sum"),
...         pl.col("bar").sort().tail(2).sum().suffix("_tail_sum"),
...     ]
... )
shape: (2, 3)
┌─────┬─────────┬──────────────┐
│ foo ┆ bar_sum ┆ bar_tail_sum │
│ --- ┆ ---     ┆ ---          │
│ str ┆ i64     ┆ i64          │
╞═════╪═════════╪══════════════╡
│ one ┆ 9       ┆ 9            │
├╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ two ┆ 6       ┆ 5            │
└─────┴─────────┴──────────────┘