polars.sum#
- polars.sum(*names: str) Expr[source]#
Sum all values.
Syntactic sugar for
col(name).sum().- Parameters:
- *names
Name(s) of the columns to use in the aggregation.
See also
Notes
If there are no non-null values, then the output is
0. If you would prefer empty sums to returnNone, you can usepl.when(pl.col(name).count()>0).then(pl.sum(name))instead ofpl.sum(name).Examples
Sum a column.
>>> df = pl.DataFrame( ... { ... "a": [1, 2], ... "b": [3, 4], ... "c": [5, 6], ... } ... ) >>> df.select(pl.sum("a")) shape: (1, 1) ┌─────┐ │ a │ │ --- │ │ i64 │ ╞═════╡ │ 3 │ └─────┘
Sum multiple columns.
>>> df.select(pl.sum("a", "c")) shape: (1, 2) ┌─────┬─────┐ │ a ┆ c │ │ --- ┆ --- │ │ i64 ┆ i64 │ ╞═════╪═════╡ │ 3 ┆ 11 │ └─────┴─────┘ >>> df.select(pl.sum("^.*[bc]$")) shape: (1, 2) ┌─────┬─────┐ │ b ┆ c │ │ --- ┆ --- │ │ i64 ┆ i64 │ ╞═════╪═════╡ │ 7 ┆ 11 │ └─────┴─────┘