Mean of DataFrame

To find the mean of the values over rows or columns in DataFrame in Pandas, call mean() method on this DataFrame. mean() method returns a Series with the mean calculated over specified axis.

In this tutorial, we will learn how to find the mean of values along index or columns of a DataFrame using DataFrame.mean() method.

Syntax

The syntax of pandas DataFrame.mean() method is

</>
Copy
DataFrame.mean(axis=None, skipna=None, level=None, numeric_only=None, **kwargs)

where

ParameterValueDescription
axis{index (0), columns (1)}Axis for the function to be applied on.
skipnabool, default TrueExclude NA/null values when computing the result.
levelint or level name, default NoneIf the axis is a MultiIndex (hierarchical), count along a particular level, collapsing into a Series.
numeric_onlybool, default NoneInclude only float, int, boolean columns. If None, will attempt to use everything, then use only numeric data. Not implemented for Series.
**kwargsAdditional keyword arguments to be passed to the function.

Return Value

  • Series or
  • DataFrame (if level specified)

Examples

Mean of DataFrame for Columns

By default, the mean is calculated for columns (axis=0) in a DataFrame.

In the following program, we take a DataFrame two columns containing numerical data, and find the mean.

Example.py

</>
Copy
import pandas as pd

df = pd.DataFrame({'a': [1, 4], 'b': [3, 4]})
result = df.mean()
print(result)

Output

a    2.5
b    3.5
dtype: float64

Mean of DataFrame for Rows

To compute the mean of DataFrame along rows, pass axis=1 in call to mean() method.

Example.py

</>
Copy
import pandas as pd

df = pd.DataFrame({'a': [1, 4], 'b': [3, 4]})
result = df.mean(axis=1)
print(result)

Output

0    2.0
1    4.0
dtype: float64

Do not skip NA while finding Mean

By default, NA values like None, np.nan, etc are ignored. But if we would like consider those values as well, pass skipna=False to mean() method.

Example.py

</>
Copy
import pandas as pd

df = pd.DataFrame({'a': [1, None], 'b': [3, 4]})
result = df.mean(skipna=False)
print(result)

Output

a    NaN
b    3.5
dtype: float64

If any of the values is NA, then the mean would be considered as NaN.

Conclusion

In this Pandas Tutorial, we learned how to find the mean of DataFrame along rows or columns using pandas DataFrame.mean() method.