Built with Tutify.io

    AIStudyLabs

    Pandas Series.mean() Function

    Pandas Series.mean() Function

    In Pandas, the Series.mean() function is used to compute the mean or average value of the elements within a Series. Upon execution, it yields a single float value, signifying the calculated mean of the series.

    In this article, I will explain the mean() function, its syntax, parameters, and usage of how to calculate the mean values of a given Series object.

    Key Points –

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

    Pandas Series.mean() Introduction

    Following is the syntax of creating Series.mean() function.

    
    # Syntax of Series.mean() function
    Series.mean(axis=_NoDefault.no_default, skipna=True, level=None, numeric_only=None, **kwargs)
    

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

    Parameters of the series.mean() Function

    Following are the parameters of the mean().

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

    Return Value

    It returns a single float value representing the mean of the elements in the Series.

    <a id="h-pandas-series-mean-usage"></a>

    Pandas Series mean() Usage

    The mean() function returns the arithmetic mean of given object elements in Pandas. Arithmetic mean is a sum of elements of given object, along with the specified axis divided by the number of elements.

    You can also specify the axis parameter to specify the axis along which the mean is calculated. By default, axis=0, which means the mean is calculated along the rows (i.e., across all the columns). If you set axis=1, the mean is calculated along the columns (i.e., across all the rows).

    To run some examples of pandas series mean function, let’s create pandas series.

    
    import pandas as pd
    import numpy as np
    
    # Create a Series
    ser = pd.Series([13, 25, 6, 10, 12, 9, 20])
    print(ser)
    

    The following example calculates the mean.

    
    # Use Series.mean() function
    ser2 = ser.mean()
    print(ser2)
    
    # Output:
    # 13.571428571428571
    

    <a id="h-series-mean-ignore-nan"></a>

    Series Mean Ignore NaN

    By default, skipna=True, which means NaN (Not a Number) values are ignored when calculating the mean. If a series contains NaN values, they are automatically excluded from the calculation.

    
    # Pandas series mean ignore nan
    ser = pd.Series([13, 25, None, 10, 12, None, 20, 30, np.nan])
    ser2 = ser.mean(skipna = True)
    print(ser2)
    
    # Output:
    # 18.333333333333332
    

    Similarly, you can also set skipna=False to include NaN values in the calculation. If your Series contains NaN values and you choose not to skip them, the mean calculation will return NaN (Not a Number) as the result.

    
    # Pandas series mean ignore nan
    ser = pd.Series([13, 25, None, 10, 12, None, 20, 30, np.nan])
    ser2 = ser.mean(skipna = False)
    print(ser2)
    
    # Output:
    # nan
    

    <a id="h-complete-example-of-series-mean-function"></a>

    Complete Example of Series.mean() Function

    
    import pandas as pd
    import numpy as np
    
    # Create a Series
    ser = pd.Series([13, 25, 6, 10, 12, 9, 20])
    print(ser)
    
    # Use Series.mean() function
    ser2 = ser.mean()
    print(ser2)
    
    # Pandas series mean ignore nan
    ser = pd.Series([13, 25, None, 10, 12, None, 20, 30, np.nan])
    ser2 = ser.mean(skipna = True)
    print(ser2)
    

    <a id="h-frequently-asked-questions-on-pandas-series-mean-function"></a>

    Frequently Asked Questions on Pandas Series.mean() Function

    The Series.mean() function calculates the arithmetic mean (average) of the values in a Pandas Series. It returns a single float value representing the average.

    To use the Series.mean() function in Pandas, simply call it on a Pandas Series. The function calculates the arithmetic mean (average) of the Series values.

    By default, Series.mean() will ignore any missing values (NaN) when calculating the mean. If you want to include NaN values and get a result of NaN if any NaN values are present, you can use the skipna parameter.

    Series.mean() only works with numeric data types like integers or floats. If your Series contains non-numeric values, you’ll get a TypeError. To handle this, ensure the Series contains only numeric data or convert it before using mean().

    Series.mean() is not directly applicable to datetime data. However, you can convert datetime data to a numeric representation (like timestamps) before calculating the mean.

    The Series.mean() function does not support weighting. To calculate a weighted mean, you can use the numpy library or write a custom calculation.

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

    Conclusion

    In this article, you have learned the mean() function and using its syntax, parameters, and usage for computing the mean or average value of a given Series object.

    Happy Learning !!

    Related Articles

    References