Pandas Tutorial Pandas References

Pandas DataFrame - mean() function



The Pandas DataFrame mean() function returns the mean of the values over the specified axis. The syntax for using this function is mentioned below:

Syntax

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

Parameters

axis Optional. Specify {0 or 'index', 1 or 'columns'}. If 0 or 'index', mean of the values are generated for each column. If 1 or 'columns', mean of the values are generated for each row. Default: 0
skipna Optional. Specify True to exclude NA/null values when computing the result. Default is True.
level Optional. Specify level (int or str). If the axis is a MultiIndex (hierarchical), count along a particular level, collapsing into a Series. A str specifies the level name.
numeric_only Optional. Specify True to include only float, int or boolean data. Default: False

Return Value

Returns mean of the values of Series or DataFrame if a level is specified.

Example: using mean() column-wise on whole DataFrame

In the example below, a DataFrame df is created. The mean() function is used to get the mean of values for each column.

import pandas as pd
import numpy as np

df = pd.DataFrame({
  "Bonus": [5, 3, 2, 4],
  "Salary": [60, 62, 65, 59]},
  index= ["John", "Marry", "Sam", "Jo"]
)

print("The DataFrame is:")
print(df)

#mean of values of all entries column-wise
print("\ndf.mean() returns:")
print(df.mean())

The output of the above code will be:

The DataFrame is:
       Bonus  Salary
John       5      60
Marry      3      62
Sam        2      65
Jo         4      59

df.mean() returns:
Bonus      3.5
Salary    61.5
dtype: float64

Example: using mean() row-wise on whole DataFrame

To perform the operation row-wise, the axis parameter can be set to 1.

import pandas as pd
import numpy as np

df = pd.DataFrame({
  "Bonus": [5, 3, 2, 4],
  "Salary": [60, 62, 65, 59]},
  index= ["John", "Marry", "Sam", "Jo"]
)

print("The DataFrame is:")
print(df)

#mean of values of all entries row-wise
print("\ndf.mean(axis=1) returns:")
print(df.mean(axis=1))

The output of the above code will be:

The DataFrame is:
       Bonus  Salary
John       5      60
Marry      3      62
Sam        2      65
Jo         4      59

df.mean(axis=1) returns:
John     32.5
Marry    32.5
Sam      33.5
Jo       31.5
dtype: float64

Example: using mean() on selected column

Instead of whole DataFrame, the mean() function can be applied on selected columns. Consider the following example.

import pandas as pd
import numpy as np

df = pd.DataFrame({
  "Bonus": [5, 3, 2, 4],
  "Last Salary": [58, 60, 63, 57],
  "Salary": [60, 62, 65, 59]},
  index= ["John", "Marry", "Sam", "Jo"]
)

print("The DataFrame is:")
print(df)

#mean of values of single column
print("\ndf['Salary'].mean() returns:")
print(df["Salary"].mean())

#mean of values of multiple columns
print("\ndf[['Salary', 'Bonus']].mean() returns:")
print(df[["Salary", "Bonus"]].mean())

The output of the above code will be:

The DataFrame is:
       Bonus  Last Salary  Salary
John       5           58      60
Marry      3           60      62
Sam        2           63      65
Jo         4           57      59

df['Salary'].mean() returns:
61.5

df[['Salary', 'Bonus']].mean() returns:
Salary    61.5
Bonus      3.5
dtype: float64

❮ Pandas DataFrame - Functions