How to Impute Missing Values with Mean in Python
Replace None values in a list with the mean of the existing values using Python's statistics module.
Python code
21 linesimport statistics
from statistics import mean
def impute_mean(values):
"""Replace None with the mean of the non-None values."""
# Filter out None to compute the mean of existing values
valid = [v for v in values if v is not None]
if not valid:
return values # nothing to impute if all are None
avg = mean(valid)
return [avg if v is None else v for v in values]
if __name__ == "__main__":
sample = [10, None, 20, None, 30]
result = impute_mean(sample)
print(f"Original: {sample}")
print(f"Imputed: {result}")
print(f"Mean of imputed: {statistics.mean(result):.2f}")
Output
Original: [10, None, 20, None, 30]
Imputed: [10, 20.0, 20, 20.0, 30]
Mean of imputed: 20.00
How it works
The function first filters out None entries to compute the mean of the valid values. If all values are None, it returns the list unchanged to avoid division by zero. Then it uses a list comprehension to replace each None with the calculated mean, leaving non-None values untouched. This pattern is common in data preprocessing where missing values need to be filled before further analysis or model training.
Common mistakes
- Not handling the case where all values are None, leading to a ZeroDivisionError when calling mean().
- Modifying the original list in-place instead of returning a new list, which can cause unintended side effects.
- Forgetting that the mean is a float, which changes the data type of the imputed values.
- Using a hardcoded mean instead of computing it dynamically from the valid values.
Variations
- Use a list comprehension with a conditional expression: `[v if v is not None else avg for v in values]`.
- Use pandas: `df['column'].fillna(df['column'].mean())` for DataFrame column imputation.
Real-world use cases
- Preprocessing a dataset for a machine learning model by filling missing feature values with the column mean.
- Handling missing sensor readings in a time-series pipeline before aggregating statistics.
- Cleaning survey response data where unanswered questions are represented as None before analysis.
Sponsored
More from ML engineering pipelines
- Bayesian Optimization in Python: A Simplified Mock Implementation medium
- Build a Data Helper Class in Python for ML Pipelines easy
- Build a Mock Random Forest Classifier in Python easy
- Champion Challenger Deployment Mock in Python easy
- Compare Model A vs Model B Metrics in Python easy
- Create a Minimal Great Expectations Suite Mock in Python easy
Keep learning
Related tutorials and quizzes for this topic.