How to obtain the mean value over a specified axis in a DataFrame

Overview

The mean() function in pandas is used to obtain the mean of the values over a specified axis in a given DataFrame.

Mathematically, the mean can be defined as the sum of all values in a dataset divided by the number of values.

Syntax

The mean() function has the following syntax:

DataFrame.mean(axis=NoDefault.no_default, skipna=True, numeric_only=None, **kwargs)
Syntax for the mean() function in pandas

Parameters

The mean() function takes the following optional parameter values:

  • axis: This represents the name of the row ( designated as 0 or 'index') or the column (designated as 1 or columns) axis from which to take the mean.
  • skipna: This takes a boolean value. It determines whether null values are to be excluded or not in the calculation of the mean.
  • numeric_only: This takes a boolean value. It determines whether only float, int, or boolean columns are included in the calculation.
  • **kwargs: This is an additional keyword argument that can be passed to the function.

Example

# A code to illustrate the mean() function in Pandas
# importing the pandas library
import pandas as pd
# creating a dataframe
df = pd.DataFrame([[1,2,3,4,5],
[1,7,5,9,0.5],
[3,11,13,14,12]],
columns=list('ABCDE'))
# printing the dataframe
print(df)
# obtaining the mean value vertically across rows
print("Mean across rows: ", df.mean())
# obtaining the mean value horizontally over columns
print("Mean across columns: ", df.mean(axis="columns"))

Explanation

  • Line 4: We import the pandas library.
  • Lines 7–10: We create the df DataFrame.
  • Line 12: We print the df DataFrame.
  • Line 15: Using the mean() function, we obtain the mean of the values running downwards across the rows (axis 0). We print the result to the console.
  • Line 18: Using the mean() function, we obtain the mean of the values running horizontally across columns (axis 1). We print the result to the console.

New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources