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.

Easy Python 3.9+ Aug 9, 2026 ML engineering pipelines 13 views 0 copies

Python code

21 lines
Python 3.9+
import 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

stdout
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

  1. Use a list comprehension with a conditional expression: `[v if v is not None else avg for v in values]`.
  2. 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

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from ML engineering pipelines

Related tutorials and quizzes for this topic.