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

Polars · Function

pl.count

pl.count creates expressions that count non-null values in one or more named columns. Its no-argument row-count form is deprecated; use pl.len() instead.

How it works

Use pl.count with one or more column names inside a selection to produce an expression for each column. It counts non-null values; calling it without columns counts rows, but that form is deprecated and pl.len() should be used instead.

Syntax

polars.count(*columns: str) → Expr

Parameters

NameTypeDescription
columnsstrOne or more column names to count.

Returns

Expr expression of data type UInt32.

Key points

  • For named columns, the function counts values that are not null.
  • The top-level function is shorthand for applying count to a column expression.
  • The columns parameter accepts one or more names, allowing several counts in one selection.
  • The resulting expression has UInt32 data type.
  • When multiple names are supplied, the result contains a separate non-null count for each column.

Common mistakes

  • The no-argument form counts rows rather than non-null values, and the documentation marks this usage as deprecated; use pl.len() for row counts.

Example: Count non-null values

import polars as pl

df = pl.DataFrame({"a": [1, 2, None], "b": [3, None, None]})
result = df.select(
    pl.count("a").alias("a_count"),
    pl.count("b").alias("b_count"),
)
print(result)

Counts non-null values independently in the selected columns.

When to use it

Count populated values

Compute completeness metrics for selected fields.

Count multiple columns

Produce several column counts in one DataFrame selection.

Build aggregation expressions

Use named-column counts in an aggregation or projection expression.

Interview questions

QWhat does pl.count("a", "b") compute?

pl.count accepts one or more column names and returns an expression for each requested column. Each expression counts the non-null values in its column.

Follow-up: How should row counts be expressed? Use pl.len(), because the no-argument pl.count() form is deprecated.

QWhat does pl.count return?

The function returns an Expr whose data type is UInt32. In a select operation, each supplied column produces its own count result.

Follow-up: What happens when a selected column contains nulls? Those null entries are excluded from that column's count.

QWhat is the relationship between pl.count and Expr.count?

pl.count is syntactic sugar for col(columns).count(). It provides a top-level shorthand for counting non-null values in named columns.

Follow-up: Which related expression should you use when you need the method form? Use pl.col("a").count().

  • Expr.count
  • len

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