polars.cumreduce#
- polars.cumreduce(
- function: Callable[[Series, Series], Series],
- exprs: Sequence[Expr | str] | Expr,
Cumulatively accumulate over multiple columns horizontally/ row wise with a left fold.
Every cumulative result is added as a separate field in a Struct column.
- Parameters:
- function
Function to apply over the accumulator and the value. Fn(acc, value) -> new_value
- exprs
Expressions to aggregate over. May also be a wildcard expression.
Examples
>>> df = pl.DataFrame( ... { ... "a": [1, 2, 3], ... "b": [3, 4, 5], ... "c": [5, 6, 7], ... } ... ) >>> df shape: (3, 3) ┌─────┬─────┬─────┐ │ a ┆ b ┆ c │ │ --- ┆ --- ┆ --- │ │ i64 ┆ i64 ┆ i64 │ ╞═════╪═════╪═════╡ │ 1 ┆ 3 ┆ 5 │ │ 2 ┆ 4 ┆ 6 │ │ 3 ┆ 5 ┆ 7 │ └─────┴─────┴─────┘
>>> df.select( ... pl.cumreduce(function=lambda acc, x: acc + x, exprs=pl.col("*")).alias( ... "cumreduce" ... ), ... ) shape: (3, 1) ┌───────────┐ │ cumreduce │ │ --- │ │ struct[3] │ ╞═══════════╡ │ {1,4,9} │ │ {2,6,12} │ │ {3,8,15} │ └───────────┘