Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

Pandas DataFrame mean() Method

In this tutorial, we will learn the Python pandas DataFrame.mean() method. This method can be used to get the mean of the values over the requested axis. It returns Series and if the level is specified, it returns the DataFrame.

The below shows the syntax of the DataFrame.mean() method.

Syntax

DataFrame.mean(axis=None, skipna=None, level=None, numeric_only=None, **kwargs)

Parameters

axis: It represents index or column axis, '0' for index and '1' for the column. When the axis=0, method applied over the index axis and when the axis=1 method applied over the column axis.

skipna: bool(True or False). The default value is None. If this parameter is True, it excludes all NA/null values when computing the result.

level: It represents the int or level name, the default value is None. If the axis is a MultiIndex (hierarchical), count along with a particular level, collapsing into a Series.

numeric_only: bool(True or False), the default is None. If this parameter is True, it includes only float, int, boolean columns.

**kwargs: Additional keyword arguments to be passed to the method.

Example: Find mean values of the DataFrame

Let's create a DataFrame and get the mean value over the index axis by assigning parameter axis=0 in the DataFrame.mean() method. See the below example.

#importing pandas as pd
import pandas as pd
#creating the DataFrame
df = pd.DataFrame({"A":[0,52,78],"B":[77,45,96],"C":[16,23,135],"D":[17, 22, 56]}) 
print("------The DataFrame is------")
print(df)
print("---------------------------")
print(df.mean(axis=0))


------The DataFrame is------
A B C D
0 0 77 16 17
1 52 45 23 22
2 78 96 135 56
---------------------------
A 43.333333
B 72.666667
C 58.000000
D 31.666667
dtype: float64

Example: Find the mean values of the DataFrame

Let's create a DataFrame and get the mean value over the column axis by assigning a parameter axis=1 in the DataFrame.mean() method. See the below example.

#importing pandas as pd
import pandas as pd
#creating the DataFrame
df = pd.DataFrame({"A":[0,52,78],"B":[77,45,96],"C":[16,23,135],"D":[17, 22, 56]}) 
print("------The DataFrame is------")
print(df)
print("---------------------------")
print(df.mean(axis=1))


------The DataFrame is------
A B C D
0 0 77 16 17
1 52 45 23 22
2 78 96 135 56
---------------------------
0 77
1 52
2 135
dtype: int64

Example: The DataFrame.mean() Method in Pandas

Let's create a DataFrame with null values and get the mean value over the index axis excluding null values by passing parameter skipna=False in the DataFrame.mean() method . It includes all NA/null values when computing the results. See the below example.

#importing pandas as pd
import pandas as pd
#creating the DataFrame
df = pd.DataFrame({"A":[0,None,78],"B":[77,45,None],"C":[16,23,None],"D":[17, 22, 56]}) 
print("------The DataFrame is------")
print(df)
print("---------------------------")
print(df.mean(axis=0,skipna=False))


------The DataFrame is------
A B C D
0 0.0 77.0 16.0 17
1 NaN 45.0 23.0 22
2 78.0 NaN NaN 56
---------------------------
A NaN
B NaN
C NaN
D 31.666667
dtype: float64

Example: Find the mean values of the DataFrame using the DataFrame.mean() method excluding null values

Create a DataFrame with null values and get the mean value over the index axis excluding null values by passing parameter skipna=True in the DataFrame.mean() method . It excludes all NA/null values when computing the results. See the below example.

#importing pandas as pd
import pandas as pd
#creating the DataFrame
df = pd.DataFrame({"A":[0,None,78],"B":[77,45,None],"C":[16,23,None],"D":[17, 22, 56]}) 
print("------The DataFrame is------")
print(df)
print("---------------------------")
print(df.mean(axis=0,skipna=True))


------The DataFrame is------
A B C D
0 0.0 77.0 16.0 17
1 NaN 45.0 23.0 22
2 78.0 NaN NaN 56
---------------------------
A 39.000000
B 61.000000
C 19.500000
D 31.666667
dtype: float64

Conclusion

In this tutorial, we learned the Python pandas DataFrame.mean() method. We learned the syntax, parameters and by applying this method on the DataFrame we solved examples and understood the DataFrame.mean() method.