Explore Library
Code Quiz

Mean Imputation With Sentinel Values

Spot the bug when imputing missing ages that are encoded as a sentinel 0.

Codepython
import pandas as pd
import numpy as np

# In this dataset, missing ages are recorded as 0
df = pd.DataFrame({'age': [25, 0, 34, 0, 52, 41]})

# Impute the missing ages with the column mean
mean_age = df['age'].mean()
df['age'] = df['age'].replace(0, mean_age)

print(df['age'].round(1).tolist())

What is the bug in this data-cleaning code?