0
0
PandasHow-ToBeginner · 3 min read

How to Use Mean in pandas: Simple Guide with Examples

Use the mean() method in pandas to calculate the average of numeric data in a DataFrame or Series. It returns the mean value for each column by default, or for the entire Series if used on one column.
📐

Syntax

The mean() method calculates the average of numeric values.

  • DataFrame.mean(axis=0, skipna=True, numeric_only=None)
  • Series.mean(skipna=True)

Parameters:

  • axis=0: Calculate mean for each column (use axis=1 for rows).
  • skipna=True: Ignore missing values (NaN) when calculating.
  • numeric_only=None: Include only numeric data by default.
python
df.mean(axis=0, skipna=True, numeric_only=None)
💻

Example

This example shows how to calculate the mean of each column in a DataFrame and the mean of a single Series.

python
import pandas as pd

data = {'math': [90, 80, 70, None], 'english': [85, 95, None, 75], 'science': [88, 92, 85, 80]}
df = pd.DataFrame(data)

# Mean of each column
mean_columns = df.mean()

# Mean of a single column (Series)
mean_math = df['math'].mean()

print('Mean of each column:')
print(mean_columns)
print('\nMean of math column:')
print(mean_math)
Output
Mean of each column: math 80.0 english 85.0 science 86.25 dtype: float64 Mean of math column: 80.0
⚠️

Common Pitfalls

Common mistakes include:

  • Not handling missing values (NaN), which can affect the mean if skipna=False.
  • Using mean() on non-numeric columns, which can cause errors or unexpected results.
  • Confusing axis=0 (columns) and axis=1 (rows) when calculating means.
python
import pandas as pd

data = {'A': [1, 2, None], 'B': ['x', 'y', 'z']}
df = pd.DataFrame(data)

# Wrong: mean on non-numeric column
try:
    print(df['B'].mean())
except Exception as e:
    print(f'Error: {e}')

# Right: mean on numeric column
print(df['A'].mean())
Output
Error: Could not convert string to float: 'x' 1.5
📊

Quick Reference

Summary tips for using mean() in pandas:

  • Use df.mean() to get mean of each numeric column.
  • Use df.mean(axis=1) to get mean of each row.
  • Missing values are ignored by default (skipna=True).
  • Apply mean() on a Series to get the average of that column.

Key Takeaways

Use mean() to calculate average values in pandas DataFrames or Series.
By default, mean() ignores missing values (NaN) when calculating.
Specify axis=0 for column-wise mean and axis=1 for row-wise mean.
Avoid applying mean() on non-numeric data to prevent errors.
Use mean() on a Series to get the average of that single column.