• Post author:
• Post category:Pandas

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 –

• The `Series.mean()` function in Pandas is utilized to compute the arithmetic mean or average of the values present in a Pandas Series.
• Calculates the mean (average) of values in a Pandas Series.
• Returns a single scalar value representing the mean.
• Automatically excludes missing/null values from the calculation.
• By default, the `skipna` parameter is set to True, excluding any missing or NaN (Not a Number) values from the computation. However, this behavior can be modified using the `skipna` parameter.

## 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)
``````

### Parameters of the series.mean() Function

Following are the parameters of the mean().

• `axis` – {0 or ‘index’, 1 or ‘columns’}, default None. Axis for the function to be applied on. If 0 or ‘index’, it computes the mean along the index (rows); if 1 or ‘columns’, it computes the mean along columns.
• `skipna` – bool, default None. If True, NA/null values are excluded from the calculation. If False, NA/null values are included in the calculation. If None, it is automatically set to True unless at least one NA/null value is present.
• `level` – int or level name, default None. If the axis is a MultiIndex (hierarchical), the level for which the mean is calculated.
• `numeric_only` – bool, default None. If True, only numeric types are included in the calculation. If False, all data types are included.
• `**kwargs` – Additional keyword arguments are passed to the function that performs the actual calculation.

### Return Value

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

## 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
``````

## 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
``````

## 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)
``````

## 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 !!