pandas DataFrame replace nan values with average of columns
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Replacing NaN values with the average of each column is one of the simplest forms of imputation in pandas. It is easy to implement and often useful for numeric preprocessing, but it also changes the distribution of the data, so it should be used intentionally rather than automatically.
The basic pandas solution
For a numeric DataFrame, the most direct pattern is:
This works because:
- '
df.mean(...)computes one mean per numeric column' - '
fillna(...)aligns those means by column name' - each
NaNis replaced with the corresponding column mean
That makes the operation concise and fully vectorized.
What the result looks like
For the DataFrame above:
- column
Amean is(1 + 2 + 4) / 3 - column
Bmean is(5 + 7) / 2 - column
Chas no missing values
So the filled result becomes:
This is usually the quickest way to handle missing numeric values when a simple average-based imputation is acceptable.
Fill selected columns only
Sometimes you do not want to apply mean imputation to every numeric column. In that case, target the columns explicitly:
This is useful when:
- some numeric columns should stay missing
- some columns represent IDs or codes rather than measured values
- you want different imputation strategies for different features
Being explicit often makes the cleaning step safer and easier to review.
Handle non-numeric columns carefully
Mean imputation makes sense only for numeric data. If your DataFrame also contains strings or categories, keep them separate:
Trying to treat all columns the same way is a common source of messy preprocessing code.
Train-test leakage warning
If you are doing machine learning, compute the column means on the training set only, then apply those same values to validation or test data.
For example:
If you compute the means separately on the test set, you leak information from the test distribution into preprocessing. That makes evaluation less trustworthy.
This is one reason scikit-learn pipelines and imputers are often preferable in production ML workflows.
Know when mean imputation is too naive
Replacing missing values with the mean is simple, but it has drawbacks:
- it reduces variance
- it can blur real patterns
- it is sensitive to outliers
- it may be a poor fit for skewed distributions
For some columns, median imputation or model-based imputation is more appropriate. The point is not that mean imputation is bad. The point is that it is a baseline, not a universal best practice.
Common Pitfalls
The biggest mistake is applying mean imputation to columns where an average has no meaningful interpretation, such as category codes or identifiers.
Another common issue is forgetting that the mean should usually come from the training data only in machine learning workflows.
People also use mean imputation on heavily skewed or outlier-dominated columns where the median would be much more stable.
Finally, if an entire column is missing, its mean may also be NaN, so fillna(df.mean()) will not magically repair that case.
Summary
- Use
df.fillna(df.mean(numeric_only=True))for simple column-wise mean imputation. - Target only the columns where average-based filling actually makes sense.
- Treat non-numeric columns with a different strategy.
- In ML workflows, compute the imputation values on the training set and reuse them for test data.
- Remember that mean imputation is simple and convenient, but not always statistically ideal.

