Column summaries
Summarise selected numeric columns in an eager DataFrame.
Polars · Function
polars.mean creates an expression that computes the mean of one or more named columns. It is shorthand for pl.col(columns).mean().
pl.mean builds an expression that computes the average for each named column when evaluated in a DataFrame operation. It is shorthand for applying mean to pl.col with the supplied column names, and multiple names produce multiple output columns.
polars.mean(*columns: str) → Expr| Name | Type | Description |
|---|---|---|
columns | str | One or more column names. |
Expr
columns argument accepts one or more names, allowing a single call to target several columns.Expr, so it is intended for use inside DataFrame or lazy query expressions.import polars as pl
df = pl.DataFrame({"a": [1, 8, 3], "b": [4, 5, 2]})
result = df.select(pl.mean("a", "b"))
print(result)Selects the mean of each named column, producing one result column per input column.
Summarise selected numeric columns in an eager DataFrame.
Build reusable aggregations in a lazy query.
Compute several column means in one select operation.
pl.mean return and when is it evaluated?It returns a Polars expression that computes the mean value for the specified columns. The expression is evaluated when used in an operation such as select.
Follow-up: How can the same operation be written without the convenience function? Use pl.col("a").mean() for one column, or apply the column expression to each desired column.
Pass one or more column names to pl.mean. Each selected column gets its own mean result, so pl.mean("a", "b") yields two output columns.
Follow-up: What does the convenience function expand to? It is syntactic sugar for pl.col(columns).mean().
The documented parameter is columns, representing one or more column names. The function returns an Expr, which can be composed into a larger Polars query.
Follow-up: How would you compute a row-wise mean instead? Use the separately documented horizontal mean functionality rather than this column-wise expression.
polars.Expr.meanpolars.mean_horizontal