How to Handle Missing Values Without Losing Information
How should you handle missing values in a dataset?
Profile missing counts and percentages for every column, figure out why values are missing, and choose a treatment based on the business meaning — not a fixed threshold like “drop anything over 50% missing.” Options include keeping nulls, adding an is_missing flag, mapping to an explicit “Unknown” category, or imputing with the median, mean, mode, or forward fill. Flag imputed values so they stay traceable.
Profile first
missing = (df.isna().sum()
.to_frame("missing_count")
.assign(missing_pct=lambda t: (t.missing_count / len(df) * 100).round(2))
.sort_values("missing_pct", ascending=False))
print(missing)Normalize placeholder values first. Strings like “N/A”, “-”, “unknown”, or sentinel numbers like 999 and -1 hide missingness and will skew the profile if left in place.
Why the values are missing matters
- MCAR (missing completely at random): unrelated to any data. Dropping rows is least risky here.
- MAR (missing at random): related to other observed fields, such as income missing more often for one age group.
- MNAR (missing not at random): related to the missing value itself, such as high earners not reporting income. Dropping these rows biases results.
Choosing a treatment
- Retain nulls when the field is optional and downstream tools handle nulls correctly.
- Add a <col>_is_missing flag when missingness itself carries information.
- Map categorical gaps to “Unknown” or “Not applicable” — and don't merge the two if they mean different things.
- Median for skewed numbers, mean for symmetric numbers, mode for categories. All of these shrink variance.
- Forward or backward fill only for ordered time series, and never across different entities.
High missingness isn't automatically useless
A referral_code column that's 60% empty is probably sparse by design — most customers didn't use one. Dropping it loses real information. A has_referral_code flag keeps the signal.
df["has_referral_code"] = df["referral_code"].notna().astype(int)
df["income_is_missing"] = df["income"].isna().astype(int)
df["income"] = df["income"].fillna(df["income"].median())In a modeling workflow, compute the median on the training data only and apply it to the test data, to avoid leakage.
Related skills
Data Analyst Toolkit includes AI-agent skills that follow this method: