Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

pandas

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

Onyejiaku Theophilus Chidalu

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"))
Implementing the mean() function

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.

RELATED TAGS

pandas

CONTRIBUTOR

Onyejiaku Theophilus Chidalu
RELATED COURSES

View all Courses

Keep Exploring