polars.dataframe.groupby.GroupBy.agg#

GroupBy.agg(aggs: IntoExpr | Iterable[IntoExpr] | None = None, *more_aggs: IntoExpr, **named_aggs: IntoExpr) DF[source]#

Compute aggregations for each group of a groupby operation.

Parameters:
aggs

Aggregations to compute for each group of the groupby operation. Accepts expression input. Strings are parsed as column names.

*more_aggs

Additional aggregations, specified as positional arguments.

**named_aggs

Additional aggregations, specified as keyword arguments. The resulting columns will be renamed to the keyword used.

Examples

Compute the sum of a column for each group.

```>>> df = pl.DataFrame(
...     {
...         "a": ["a", "b", "a", "b", "c"],
...         "b": [1, 2, 1, 3, 3],
...         "c": [5, 4, 3, 2, 1],
...     }
... )
>>> df.groupby("a").agg(pl.col("b").sum())
shape: (3, 2)
┌─────┬─────┐
│ a   ┆ b   │
│ --- ┆ --- │
│ str ┆ i64 │
╞═════╪═════╡
│ a   ┆ 2   │
│ b   ┆ 5   │
│ c   ┆ 3   │
└─────┴─────┘
```

Compute multiple aggregates at once by passing a list of expressions.

```>>> df.groupby("a").agg([pl.sum("b"), pl.mean("c")])
shape: (3, 3)
┌─────┬─────┬─────┐
│ a   ┆ b   ┆ c   │
│ --- ┆ --- ┆ --- │
│ str ┆ i64 ┆ f64 │
╞═════╪═════╪═════╡
│ c   ┆ 3   ┆ 1.0 │
│ a   ┆ 2   ┆ 4.0 │
│ b   ┆ 5   ┆ 3.0 │
└─────┴─────┴─────┘
```

Or use positional arguments to compute multiple aggregations in the same way.

```>>> df.groupby("a").agg(
...     pl.sum("b").suffix("_sum"),
...     (pl.col("c") ** 2).mean().suffix("_mean_squared"),
... )
shape: (3, 3)
┌─────┬───────┬────────────────┐
│ a   ┆ b_sum ┆ c_mean_squared │
│ --- ┆ ---   ┆ ---            │
│ str ┆ i64   ┆ f64            │
╞═════╪═══════╪════════════════╡
│ a   ┆ 2     ┆ 17.0           │
│ c   ┆ 3     ┆ 1.0            │
│ b   ┆ 5     ┆ 10.0           │
└─────┴───────┴────────────────┘
```

Use keyword arguments to easily name your expression inputs.

```>>> df.groupby("a").agg(
...     b_sum=pl.sum("b"),
...     c_mean_squared=(pl.col("c") ** 2).mean(),
... )
shape: (3, 3)
┌─────┬───────┬────────────────┐
│ a   ┆ b_sum ┆ c_mean_squared │
│ --- ┆ ---   ┆ ---            │
│ str ┆ i64   ┆ f64            │
╞═════╪═══════╪════════════════╡
│ a   ┆ 2     ┆ 17.0           │
│ c   ┆ 3     ┆ 1.0            │
│ b   ┆ 5     ┆ 10.0           │
└─────┴───────┴────────────────┘
```