Built with Tutify.io

    AIStudyLabs

    Polars.DataFrame.mean() – Explained by Examples

    Polars.DataFrame.mean() – Explained by Examples

    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 –

    <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.

    <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,

    <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,

    <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,

    <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,

    <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,

    <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