polars.cum_count#

polars.cum_count(*columns: str, reverse: bool = False) Expr[source]#

Return the cumulative count of the non-null values in the column.

This function is syntactic sugar for col(columns).cum_count().

Parameters:
*columns

Name(s) of the columns to use.

reverse

Reverse the operation.

Examples

>>> df = pl.DataFrame({"a": [1, 2, None], "b": [3, None, None]})
>>> df.with_columns(
...     ca=pl.cum_count("a"),
...     cb=pl.cum_count("b"),
... )
shape: (3, 4)
┌──────┬──────┬─────┬─────┐
│ a    ┆ b    ┆ ca  ┆ cb  │
│ ---  ┆ ---  ┆ --- ┆ --- │
│ i64  ┆ i64  ┆ u32 ┆ u32 │
╞══════╪══════╪═════╪═════╡
│ 1    ┆ 3    ┆ 1   ┆ 1   │
│ 2    ┆ null ┆ 2   ┆ 1   │
│ null ┆ null ┆ 2   ┆ 1   │
└──────┴──────┴─────┴─────┘