polars.lazyframe.groupby.LazyGroupBy.agg#
- LazyGroupBy.agg(aggs: IntoExpr | Iterable[IntoExpr] | None = None, *more_aggs: IntoExpr, **named_aggs: IntoExpr) LDF [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.
>>> ldf = pl.DataFrame( ... { ... "a": ["a", "b", "a", "b", "c"], ... "b": [1, 2, 1, 3, 3], ... "c": [5, 4, 3, 2, 1], ... } ... ).lazy() >>> ldf.groupby("a").agg(pl.col("b").sum()).collect() shape: (3, 2) ┌─────┬─────┐ │ a ┆ b │ │ --- ┆ --- │ │ str ┆ i64 │ ╞═════╪═════╡ │ a ┆ 2 │ │ b ┆ 5 │ │ c ┆ 3 │ └─────┴─────┘
Compute multiple aggregates at once by passing a list of expressions.
>>> ldf.groupby("a").agg( ... [pl.sum("b"), pl.mean("c")] ... ).collect() 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.
>>> ldf.groupby("a").agg( ... pl.sum("b").suffix("_sum"), ... (pl.col("c") ** 2).mean().suffix("_mean_squared"), ... ).collect() 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.
>>> ldf.groupby("a").agg( ... b_sum=pl.sum("b"), ... c_mean_squared=(pl.col("c") ** 2).mean(), ... ).collect() shape: (3, 3) ┌─────┬───────┬────────────────┐ │ a ┆ b_sum ┆ c_mean_squared │ │ --- ┆ --- ┆ --- │ │ str ┆ i64 ┆ f64 │ ╞═════╪═══════╪════════════════╡ │ a ┆ 2 ┆ 17.0 │ │ c ┆ 3 ┆ 1.0 │ │ b ┆ 5 ┆ 10.0 │ └─────┴───────┴────────────────┘