polars.cumsum#
- polars.cumsum(column: str | Sequence[Expr | str] | Expr) Expr [source]#
- polars.cumsum(column: Series) int | float
Cumulatively sum values in a column/Series, or horizontally across list of columns/expressions.
pl.cumsum(str)
is syntactic sugar for:>>> pl.col(str).cumsum()
pl.cumsum(list)
is syntactic sugar for:>>> pl.cumfold(pl.lit(0), lambda x, y: x + y, list).alias( ... "cumsum" ... )
- Parameters:
- column
Column(s) to be used in aggregation. This can be:
a column name, or Series -> aggregate the sum value of that column/Series.
a List[Expr] -> aggregate the sum value horizontally across the Expr result.
Examples
>>> df = pl.DataFrame( ... { ... "a": [1, 2], ... "b": [3, 4], ... "c": [5, 6], ... } ... ) >>> df shape: (2, 3) ┌─────┬─────┬─────┐ │ a ┆ b ┆ c │ │ --- ┆ --- ┆ --- │ │ i64 ┆ i64 ┆ i64 │ ╞═════╪═════╪═════╡ │ 1 ┆ 3 ┆ 5 │ │ 2 ┆ 4 ┆ 6 │ └─────┴─────┴─────┘
Cumulatively sum a column by name:
>>> df.select(pl.cumsum("a")) shape: (2, 1) ┌─────┐ │ a │ │ --- │ │ i64 │ ╞═════╡ │ 1 │ │ 3 │ └─────┘
Cumulatively sum a list of columns/expressions horizontally:
>>> df.with_columns(pl.cumsum(["a", "c"])) shape: (2, 4) ┌─────┬─────┬─────┬───────────┐ │ a ┆ b ┆ c ┆ cumsum │ │ --- ┆ --- ┆ --- ┆ --- │ │ i64 ┆ i64 ┆ i64 ┆ struct[2] │ ╞═════╪═════╪═════╪═══════════╡ │ 1 ┆ 3 ┆ 5 ┆ {1,6} │ │ 2 ┆ 4 ┆ 6 ┆ {2,8} │ └─────┴─────┴─────┴───────────┘