Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Polars · Function

pl.sum

polars.sum creates an expression that sums all values in one or more named columns. It is shorthand for col(name).sum() and supports multiple names or a regular-expression selector.

How it works

pl.sum constructs an expression that aggregates every value in one or more named columns. It is shorthand for selecting each column with pl.col(name).sum(), and it can also accept a regular-expression selector to choose matching columns.

Syntax

polars.sum(**names: str) → Expr

Parameters

NameTypeDescription
namesstrName or names of the columns to use in the aggregation.

Returns

Expr

Key points

  • Creates an expression that reduces each selected column to its total.
  • For one named column, it is a concise alias for the corresponding column expression's sum method.
  • The names argument identifies the columns whose values are aggregated.
  • Multiple names produce multiple output columns, with one sum for each input column.
  • A column containing only null values yields zero.
  • A count-guarded conditional can change the all-null result from zero to None.

Common mistakes

  • An entirely null input produces 0 rather than None; use a count-based conditional when an empty sum should remain null.

Example: Sum selected columns

import polars as pl

df = pl.DataFrame({"a": [1, 2], "b": [3, 4], "c": [5, 6]})
result = df.select(pl.sum("a", "c"))
print(result)

Builds an expression that sums columns a and c, then evaluates it with select.

When to use it

Column total

Select a single aggregate total from a DataFrame column.

Multiple column totals

Compute independent totals for several numeric columns in one select operation.

Pattern-based selection

Choose columns by a naming pattern when the schema is dynamic.

Interview questions

QWhat does pl.sum return, and what is its relationship to pl.col(name).sum()?

pl.sum returns an Expr representing a column aggregation, rather than immediately computing a Python scalar. A single column name is equivalent to pl.col(name).sum().

Follow-up: How would you preserve null for an all-null column? Use pl.when(pl.col(name).count()>0).then(pl.sum(name)).otherwise(None).then(pl.sum(name)) is not required; the documented pattern uses the count condition around the sum.

QHow can pl.sum aggregate more than one column?

Pass multiple column names to create one result per named column. The documented examples also show a regular-expression selector such as pl.sum("^.*[bc]$") to select matching columns.

Follow-up: When should a row-wise operation be used instead? Use pl.sum_horizontal for horizontal aggregation across columns in each row.

QWhat happens when a selected column has only null values?

The function sums all values in the selected columns and returns an Expr. If a selected column contains no non-null values, its result is 0.

Follow-up: What alternative is documented for empty sums? Wrap the sum in a count check so the expression returns None when the non-null count is zero.

  • polars.Expr.sum
  • polars.Series.sum
  • polars.sum_horizontal

Source: polars.sum — Polars documentation. Checked 2026-10-15.