Count populated values
Compute completeness metrics for selected fields.
Polars · Function
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.
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.
polars.count(*columns: str) → Expr| Name | Type | Description |
|---|---|---|
columns | str | One or more column names to count. |
Expr expression of data type UInt32.
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.
Compute completeness metrics for selected fields.
Produce several column counts in one DataFrame selection.
Use named-column counts in an aggregation or projection expression.
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.
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.
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.countlen