polars.Expr.agg_groups

Expr.agg_groups() polars.internals.expr.Expr

Get the group indexes of the group by operation. Should be used in aggregation context only.

Examples

>>> df = pl.DataFrame(
...     {
...         "group": [
...             "one",
...             "one",
...             "one",
...             "two",
...             "two",
...             "two",
...         ],
...         "value": [94, 95, 96, 97, 97, 99],
...     }
... )
>>> df.groupby("group").agg(pl.col("value").agg_groups())
shape: (2, 2)
┌───────┬────────────┐
│ group ┆ value      │
│ ---   ┆ ---        │
│ str   ┆ list [u32] │
╞═══════╪════════════╡
│ two   ┆ [3, 4, 5]  │
├╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┤
│ one   ┆ [0, 1, 2]  │
└───────┴────────────┘