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 theskipna
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)
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.
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
- Pandas Series.fillna() Function
- How to Reshape Pandas Series
- Pandas Series rank() Function
- Pandas Series mode() Function
- Pandas Series count() Function
- Pandas Series.rolling() Function
- Pandas Series sample() Function
- Pandas Series reset_index() Function
- How to Convert List to Pandas Series
- Add Column Name to Pandas Series
- Create a Set From a Series in Pandas
- Series.str.contains() With Examples
- Series.value_counts() With Examples
- Apply Multiple Filters to DataFrame or Series
- Convert GroupBy output from Series to DataFrame