Pandas Series.mean() Function

The Pandas Series.mean() method is used to calculate the mean or average of the values. It returns a float value representing the mean of the series. In this article, I will explain the syntax of Series.mean() function, its parameters, and how to compute the mean values of a given Series object with examples.

1. Syntax of Series.mean() Function

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)

Following are the parameters of the mean().

  • axis – {index (0)}: Specify the axis for the function to be applied on. For Series, this parameter is unused and defaults to 0.
  • skipna – bool, default True: Excludes all None/NaN from the mean/computing the result. Default set to True
  • level – Use with multiindex. Takes int or level name, default None
  • numeric_only – Excludes all non-numeric values. Considers only int, float & boolean. Default None
  • **kwargs – Additional keyword arguments to be passed to the function.

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

Now, let’s create pandas series using a list of values.


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)

Yields below output.


# Output
13.571428571428571

3. Series Mean Ignore NaN

By default skipna=True meaning it ignores the NaN (Not a Number) values 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

You can also use the skipna=False to not ignore NaN values, and if you have Nan values in the series it returns nan values.


# 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

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

7. Conclusion

In this article, I have explained the pandas series mean() function that returns the mean of values of a given series object with examples.

Happy Learning !!

Related Articles

References

Leave a Reply