Built with Tutify.io

    AIStudyLabs

    Polars Series mean() – Usage & Examples

    Polars Series mean() – Usage & Examples

    In Polars, the Series.mean() function is used to compute the arithmetic mean (average) of all non-null numeric values in a Series. In this article, I will explain the mean() method of the Polars Series, including its syntax, parameters, and functionality. This method returns a float when the Series contains at least one non-null numeric value, and None if the Series is empty or consists entirely of nulls.

    Key Points –

    <a id="h-polars-series-mean-introduction"></a>

    Polars Series mean() Introduction

    Let’s know the syntax of the series mean() method.

    
    # Syntax of Series mean()
    Series.mean() → PythonLiteral | None
    

    <a id="h-parameters-of-the-polars-series-mean"></a>

    Parameters of the Polars series mean()

    It allows only one parameter.

    <a id="h-return-value"></a>

    Return Value

    This function returns a float if the Series has at least one non-null numeric value. None if all values are null or the Series is empty.

    <a id="h-usage-of-polars-series-mean-method"></a>

    Usage of Polars Series mean() Method

    The Series.mean() function in Polars calculates the arithmetic mean (average) of the values in a Series.

    First, let’s create a Polars Series.

    
    import polars as pl
    
    # Sample Series with nulls
    ser = pl.Series("values", [10, 20, 30, 40, 50])
    print("Original Series:\n", ser)
    

    Yields below output.

    The mean() method in Polars can be used to calculate the average of all numeric values in a Series. Here's a basic example demonstrating how to use Series.mean() to compute the mean of integer values.

    
    # Calculate the mean
    mean_value = ser.mean()
    print("Mean of the Series:", mean_value)
    

    Here,

    <a id="h-series-with-none-values-nulls"></a>

    Series with None Values (Nulls)

    The Series.mean() method in Polars gracefully handles Series containing None (null) values. Below is an example showing how it computes the mean while ignoring nulls.

    
    import polars as pl
    
    # Create a Series with some None (null) values
    ser = pl.Series("values_with_nulls", [10, None, 30, None, 50])
    
    # Calculate the mean
    mean_value = ser.mean()
    print("\nMean of the Series (excluding nulls):", mean_value)
    
    # Output:
    # Mean of the Series (excluding nulls): 30.0
    

    Here,

    <a id="h-series-with-all-none-values"></a>

    Series with All None Values

    Using the Polars Series.mean() method on a Series where all values are None (null) returns None.

    
    import polars as pl
    
    # Create a Series where all values are None
    ser = pl.Series("all_nulls", [None, None, None])
    
    # Calculate the mean
    mean_value = ser.mean()
    print("\nMean of the Series:", mean_value)
    
    # Output:
    # Mean of the Series: None
    

    Here,

    <a id="h-series-with-negative-and-positive-numbers"></a>

    Series with Negative and Positive Numbers

    You can also use the Polars Series.mean() method with a combination of negative and positive numbers. It accurately computes the mean regardless of the sign of the values.

    
    import polars as pl
    
    # Create a Series with both negative and positive values
    ser = pl.Series("mixed_values", [-10, -5, 0, 5, 10])
    
    # Calculate the mean
    mean_value = ser.mean()
    print("\nMean of the Series:", mean_value)
    
    # Output:
    # Mean of the Series: 0.0
    

    Here,

    <a id="h-conclusion"></a>

    Conclusion

    In conclusion, the Polars Series.mean() method is a simple yet powerful tool for calculating the average of numeric values in a Series. It handles nulls gracefully by ignoring them, returns None for empty or all-null Series, and works seamlessly with both positive and negative numbers.

    Happy Learning!!

    <a id="h-related-articles"></a>

    Related Articles

    <a id="h-references"></a>

    References