polars.Expr.agg_groups#
- Expr.agg_groups() Expr[source]#
Get the group indexes of the group by operation.
Deprecated since version 1.35: use
df.with_row_index().group_by(...).agg(pl.col('index'))instead. This method will be removed in Polars 2.0.Should be used in aggregation context only.
Examples
>>> import warnings >>> warnings.filterwarnings("ignore", category=DeprecationWarning) >>> df = pl.DataFrame( ... { ... "group": [ ... "one", ... "one", ... "one", ... "two", ... "two", ... "two", ... ], ... "value": [94, 95, 96, 97, 97, 99], ... } ... ) >>> df.group_by("group", maintain_order=True).agg(pl.col("value").agg_groups()) shape: (2, 2) ┌───────┬───────────┐ │ group ┆ value │ │ --- ┆ --- │ │ str ┆ list[u32] │ ╞═══════╪═══════════╡ │ one ┆ [0, 1, 2] │ │ two ┆ [3, 4, 5] │ └───────┴───────────┘
New recommended approach: >>> ( … df.with_row_index() … .group_by(“group”, maintain_order=True) … .agg(pl.col(“index”)) … ) shape: (2, 2) ┌───────┬───────────┐ │ group ┆ index │ │ — ┆ — │ │ str ┆ list[u32] │ ╞═══════╪═══════════╡ │ one ┆ [0, 1, 2] │ │ two ┆ [3, 4, 5] │ └───────┴───────────┘