polars.sum#

polars.sum(exprs: Series) int | float[source]#
polars.sum(exprs: IntoExpr | Iterable[IntoExpr], *more_exprs: IntoExpr) Expr

Sum all values.

If a single string is passed, this is an alias for pl.col(name).sum().

If a single Series is passed, this is an alias for Series.sum(). This functionality is deprecated.

Otherwise, this function computes the sum horizontally across multiple columns. This functionality is deprecated, use pl.sum_horizontal instead.

Parameters:
exprs

Column(s) to use in the aggregation. Accepts expression input. Strings are parsed as column names, other non-expression inputs are parsed as literals.

*more_exprs

Additional columns to use in the aggregation, specified as positional arguments.

See also

sum_horizontal

Examples

Sum a column by name:

>>> df = pl.DataFrame(
...     {
...         "a": [1, 2],
...         "b": [3, 4],
...         "c": [5, 6],
...     }
... )
>>> df.select(pl.sum("a"))
shape: (1, 1)
┌─────┐
│ a   │
│ --- │
│ i64 │
╞═════╡
│ 3   │
└─────┘

To aggregate the sums for more than one column/expression use pl.col(list).sum() or a regular expression selector like pl.sum(regex):

>>> df.select(pl.col("a", "c").sum())
shape: (1, 2)
┌─────┬─────┐
│ a   ┆ c   │
│ --- ┆ --- │
│ i64 ┆ i64 │
╞═════╪═════╡
│ 3   ┆ 11  │
└─────┴─────┘
>>> df.select(pl.sum("^.*[bc]$"))
shape: (1, 2)
┌─────┬─────┐
│ b   ┆ c   │
│ --- ┆ --- │
│ i64 ┆ i64 │
╞═════╪═════╡
│ 7   ┆ 11  │
└─────┴─────┘