🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpandas

pandas Documentation

LOADING ENGINE...

df.mean()

AI & DATA SCIENCE // df-mean

df.mean() computes the arithmetic average of each column (or, after a groupby(), each group), skipping missing values by default.

Syntax

df.mean(axis=0, skipna=True, numeric_only=False)

Deep Dive Course

mean() shares sum()'s default axis and skipna behavior, but divides by the count of non-null values rather than the total row count, so a column's mean is computed only over the values that are actually present. Calling mean() directly on a DataFrame with non-numeric columns can raise an error, or, depending on the pandas version, silently skip them, which is why numeric_only=True is sometimes needed explicitly to restrict the calculation to columns where a mean actually makes sense.

1Understanding df.mean()

mean() shares sum()'s default axis and skipna behavior, but divides by the count of non-null values rather than the total row count, so a column's mean is computed only over the values that are actually present. Calling mean() directly on a DataFrame with non-numeric columns can raise an error, or, depending on the pandas version, silently skip them, which is why numeric_only=True is sometimes needed explicitly to restrict the calculation to columns where a mean actually makes sense.

💡

Pass numeric_only=True when calling mean(), or similar aggregations, directly on a DataFrame that has non-numeric columns mixed in — otherwise the call can raise an error, or its behavior can vary depending on the pandas version, since a mean isn't meaningful for text columns.

editor.html
import pandas as pd

df = pd.DataFrame({"a": [1, 2, 3], "b": [10, 20, 30]})
print(df.mean())
localhost:3000

2Practical Example

Here is a real-world application of df.mean() showing how it is used in production Pandas code.

editor.html
import pandas as pd

df = pd.DataFrame({"team": ["A", "A", "B"], "score": [10, 20, 30]})
print(df.groupby("team")["score"].mean())
localhost:3000

3Best Practices

Follow these guidelines when working with df.mean():

1. Chain mean() after groupby() for per-group averages, the pandas equivalent of a SQL GROUP BY with AVG()

2. Pass numeric_only=True when calling mean() on a DataFrame that mixes numeric and non-numeric columns

3. Be aware NaN values are excluded from both the sum and the count used to compute the mean by default, rather than treated as 0

⚠️

Tip: Pass numeric_only=True when calling mean(), or similar aggregations, directly on a DataFrame that has non-numeric columns mixed in — otherwise the call can raise an error, or its behavior can vary depending on the pandas version, since a mean isn't meaningful for text columns.

editor.html
import pandas as pd

df = pd.DataFrame({"a": [1, 2, 3], "b": [10, 20, 30]})
print(df.mean())
localhost:3000

Examples

Example 01Basic Usage
import pandas as pd

df = pd.DataFrame({"a": [1, 2, 3], "b": [10, 20, 30]})
print(df.mean())
Example 02Advanced Example
import pandas as pd

df = pd.DataFrame({"team": ["A", "A", "B"], "score": [10, 20, 30]})
print(df.groupby("team")["score"].mean())

Best Practices

  • Chain mean() after groupby() for per-group averages, the pandas equivalent of a SQL GROUP BY with AVG()
  • Pass numeric_only=True when calling mean() on a DataFrame that mixes numeric and non-numeric columns
  • Be aware NaN values are excluded from both the sum and the count used to compute the mean by default, rather than treated as 0

Interview Question

Why might calling df.mean() directly on a DataFrame raise an error or behave inconsistently if the DataFrame has a text column mixed in with numeric ones?

Hint: Think about what mean() would have to compute for a column of strings.

There's no meaningful way to compute an arithmetic average of a column of text values — averaging requires numbers, and pandas has no sensible default for what a mean of strings would even represent. Depending on the pandas version, calling mean() on a DataFrame with mixed column types either raises a TypeError or silently restricts the calculation to just the numeric columns, which is why passing numeric_only=True explicitly is the clearer, version-independent way to state that intent directly, rather than relying on whatever the current version's default handling happens to be.

Exercises

MediumPractice using df.mean() in a real scenario.
View Solution
import pandas as pd

df = pd.DataFrame({"a": [1, 2, 3], "b": [10, 20, 30]})
print(df.mean())

Frequently Asked Questions

Why might calling df.mean() directly on a DataFrame raise an error or behave inconsistently if the DataFrame has a text column mixed in with numeric ones?

There's no meaningful way to compute an arithmetic average of a column of text values — averaging requires numbers, and pandas has no sensible default for what a mean of strings would even represent. Depending on the pandas version, calling mean() on a DataFrame with mixed column types either raises a TypeError or silently restricts the calculation to just the numeric columns, which is why passing numeric_only=True explicitly is the clearer, version-independent way to state that intent directly, rather than relying on whatever the current version's default handling happens to be.

Related Functions

df-sumdf-countdf-groupby