polars.sum_horizontal#

polars.sum_horizontal(*exprs: IntoExpr | Iterable[IntoExpr], ignore_nulls: bool = True) Expr[source]#

Sum all values horizontally across columns.

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.

ignore_nulls

Ignore null values (default). If set to False, any null value in the input will lead to a null output.

Examples

>>> df = pl.DataFrame(
...     {
...         "a": [1, 8, 3],
...         "b": [4, 5, None],
...         "c": ["x", "y", "z"],
...     }
... )
>>> df.with_columns(sum=pl.sum_horizontal("a", "b"))
shape: (3, 4)
┌─────┬──────┬─────┬─────┐
│ a   ┆ b    ┆ c   ┆ sum │
│ --- ┆ ---  ┆ --- ┆ --- │
│ i64 ┆ i64  ┆ str ┆ i64 │
╞═════╪══════╪═════╪═════╡
│ 1   ┆ 4    ┆ x   ┆ 5   │
│ 8   ┆ 5    ┆ y   ┆ 13  │
│ 3   ┆ null ┆ z   ┆ 3   │
└─────┴──────┴─────┴─────┘