polars.Expr.cumcount#

Expr.cumcount(*, reverse: bool = False) Self[source]#

Get an array with the cumulative count computed at every element.

Counting from 0 to len

Parameters:
reverse

Reverse the operation.

Examples

>>> df = pl.DataFrame({"a": [1, 2, 3, 4]})
>>> df.select(
...     [
...         pl.col("a").cumcount(),
...         pl.col("a").cumcount(reverse=True).alias("a_reverse"),
...     ]
... )
shape: (4, 2)
┌─────┬───────────┐
│ a   ┆ a_reverse │
│ --- ┆ ---       │
│ u32 ┆ u32       │
╞═════╪═══════════╡
│ 0   ┆ 3         │
│ 1   ┆ 2         │
│ 2   ┆ 1         │
│ 3   ┆ 0         │
└─────┴───────────┘