Pandas DataFrame mean() Method

Pandas DataFrame mean() method calculates the mean (average) of the elements in each column (by default) or along the rows. You can specify the axis along which you want to compute the mean.

To find the mean of DataFrame, you can use the “DataFrame.mean()” function. If the mean() method is applied to a series object, it returns the scalar value, which is the mean value of all the values in the DataFrame. If the mean() method is applied to a DataFrame object, and it returns the series object that contains the mean of the values over the specified axis.


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


axis{index (0), columns (1)}

Axis for the method to be applied.

skipna: bool, default True

Exclude NA/None values when computing the result.

level: int or level name, default None

If the axis is the MultiIndex, count along with a specific level, collapsing into the Series.

numeric_only: bool, default None

Include only float, int, and boolean columns. If the values are None, I will attempt to use everything, then use only numeric data. However, I have not implemented it for Series.


Additional keyword arguments are to be passed to the function.

Return Value

It returns Series or DataFrame (if level specified).


import pandas as pd

data = {
  "A": [1, 2, 3],
  "B": [4, 5, 6],
  "C": [7, 8, 9]

df = pd.DataFrame(data)

column_mean = df.mean()
print("Mean along columns:")

row_mean = df.mean(axis=1)
print("\nMean along rows:")


Mean along columns:
A   2.0
B   5.0
C   8.0
dtype: float64

Mean along rows:
0   4.0
1   5.0
2   6.0
dtype: float64

In this code, we created a sample DataFrame and used the mean() method to calculate the mean along the columns (default) and the rows (by setting axis=1).

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.