In Polars, the mean() function is used to calculate the mean (average) of all numeric columns in DataFrame or Series. It automatically excludes non-numeric columns and returns a single-row DataFrame containing the mean values. Missing values are handled gracefully. This function can be applied to the entire DataFrame, specific columns, or filtering rows.
In this article, I will explain the Polars DataFrame mean() function and by using its syntax, parameters, and usage how we can generate a new DataFrame with the mean of all numeric columns, excluding non-numeric columns like strings.
Key Points –
- It calculates the average for all numerical columns in a Polars DataFrame while ignoring non-numeric columns.
- The result is a DataFrame with a single row containing the mean values of numeric columns.
- By default, it skips missing values when computing the mean instead of raising an error.
- The operation is non-destructive; it does not modify the existing DataFrame but returns a new one.
- Works on integer (
i64), floating-point (f64), and mixed numeric data types, converting integers to float for accurate results. - Instead of computing the mean for all numeric columns, you can use
.select()to apply it to specific ones.
<a id="h-polars-dataframe-mean-introduction"></a>
Polars DataFrame mean() Introduction
Let’s know the syntax of the mean() function.
# Syntax of mean()
polars.mean(*columns: str) → Expr
<a id="h-parameters-of-the-polars-dataframe-mean"></a>
Parameters of the Polars DataFrame.mean()
It allows only one parameter.
*columns- This computes the mean of all numeric columns in the DataFrame.
<a id="h-return-value"></a>
Return Value
This function returns a new DataFrame containing the mean of all numeric columns, while ignoring non-numeric columns such as strings.
<a id="h-usage-of-polars-dataframe-mean-function"></a>
Usage of Polars DataFrame mean() Function
The mean() function in Polars computes the average of numeric columns in a DataFrame. It works similarly to Pandas' mean() but is optimized for performance in Polars.
First, let’s create a polars DataFrame and learn how to use the mean with an example.
import polars as pl
technologies = ({
'Courses':["Spark", "pandas","Java","Spark"],
'Fee' :[20000,25000,30000,22000],
'Discount':[1000,2500,1500,3000]
})
df = pl.DataFrame(technologies)
print("Original DataFrame:\n",df)
Yields below output.
You can use the mean() method to compute the mean (average) of all numeric columns in a Polars DataFrame. Non-numeric columns (like strings) are ignored automatically.
# Compute mean of all numeric columns
df2 = df.mean()
print("Mean of Numeric Columns:\n", df2)
Here,
- Computes the mean only for numeric columns (
FeeandDiscount). - Non-numeric columns like
Coursesare ignored. - The result is a single-row DataFrame containing the mean of all numeric columns.
<a id="h-compute-mean-of-a-specific-column"></a>
Compute Mean of a Specific Column
To compute the mean (average) of a specific column in a Polars DataFrame, use the select() method along with pl.col("column_name").mean().
# Compute mean of a specific column (Fee)
df2 = df.select(pl.col("Fee").mean())
print("Mean of Fee Column:\n", df2)
# Compute mean of a single column (Fee)
df2 = df.select(pl.mean("Fee"))
print("Mean of Fee Column:\n", df2)
# Output:
# Mean of Fee Column:
# shape: (1, 1)
┌─────────┐
│ Fee │
│ --- │
│ f64 │
╞═════════╡
│ 24250.0 │
└─────────┘
Here,
pl.col("Fee").mean()selects the"Fee"column and calculates its mean.select()is used to return a new DataFrame with the result.- The output is a single-row DataFrame containing the mean value.
- Non-numeric columns (like
"Courses") are automatically ignored.
<a id="h-mean-of-a-single-column"></a>
Mean of a Single Column
Alternatively, you can calculate the mean (average) of a specific column and obtain a single numeric value (scalar) in Polars.
# Compute mean of the 'Discount' column and return a scalar
df2 = df["Discount"].mean()
print("Mean of 'Fee':", df2)
# Output:
# Mean of 'Fee': 2000.0
Here,
df["Discount"]extracts the"Discount"column as a Polars Series.mean()computes the mean of the Series.- The result is a scalar value (float) rather than a DataFrame.
<a id="h-compute-mean-after-filtering-rows"></a>
Compute Mean After Filtering Rows
To compute the mean after filtering rows in a Polars DataFrame, you can use the filter() method before applying the mean() function.
# Filter rows where Fee is greater than 21000, then compute mean
df2 = df.filter(pl.col("Fee") > 21000).mean()
print("Mean After Filtering Rows:\n", df2)
# Output:
# Mean After Filtering Rows:
# shape: (1, 3)
┌─────────┬──────────────┬─────────────┐
│ Courses ┆ Fee ┆ Discount │
│ --- ┆ --- ┆ --- │
│ str ┆ f64 ┆ f64 │
╞═════════╪══════════════╪═════════════╡
│ null ┆ 25666.666667 ┆ 2333.333333 │
└─────────┴──────────────┴─────────────┘
Here,
- We apply
filter(pl.col("Fee") > 21000)before computing the mean. - The mean is calculated only for the filtered rows.
- Non-numeric columns (like
Courses) are ignored.
<a id="h-compute-mean-with-missing-values-null-handling"></a>
Compute Mean with Missing Values (null Handling)
Similarly, to compute the mean while handling missing values (nulls) in a Polars DataFrame, you can use the mean() method. By default, Polars ignores null values when computing the mean.
# Compute mean while ignoring nulls
df2 = df.mean()
print("Mean with Missing Values:\n", df2)
# Output:
# Mean with Missing Values:
# shape: (1, 3)
┌─────────┬─────────┬─────────────┐
│ Courses ┆ Fee ┆ Discount │
│ --- ┆ --- ┆ --- │
│ str ┆ f64 ┆ f64 │
╞═════════╪═════════╪═════════════╡
│ null ┆ 24000.0 ┆ 2166.666667 │
└─────────┴─────────┴─────────────┘
Here,
- The mean calculation excludes
Nonevalues. - Unlike some libraries, Polars does not return
NaNfor columns with missing values, it computes the mean of available values. - Works efficiently even with large datasets containing missing data.
<a id="h-conclusion"></a>
Conclusion
In this article, I have explained the Polars DataFrame mean() function, including its syntax, parameters, usage, and how it returns a new DataFrame with the mean of all numeric columns, while ignoring non-numeric columns.
Happy Learning!!
<a id="h-related-articles"></a>
Related Articles
- Polars DataFrame limit() Method
- Polars DataFrame describe() Function
- How to drop a column
- Polars Cast Multiple Columns
- How to Transpose DataFrame
- DataFrame tail() – Usage & Examples
- DataFrame update() – Usage & Examples
- DataFrame fill_null() Usage & Examples
- Polars DataFrame std() – Usage & Examples
