How do you handle missing values in a dataset?
First understand why values are missing. Then drop rows or columns if few are affected, or impute with mean, median, mode, a constant, or a model-based estimate. Adding a 'was missing' flag often helps. Fit the imputer on training data only.
Explain it simply
A survey form comes back with some fields blank. You can throw away incomplete forms, fill blanks with a sensible guess, or note that the field was left blank, because leaving it blank may itself mean something. A person who skips the "salary" question may differ from one who answers.
Example
Age: 22, 25, NaN, 24, 29
Mean of known values = (22+25+24+29)/4 = 25.0
Fill NaN with 25.0 (mean) or 24.5 (median)Options:
- Drop rows when only a tiny fraction is missing and it is random.
- Drop a column when most of it is empty (for example more than 60-70% missing).
- Simple imputation: mean or median for numbers, most frequent for categories.
- Model-based:
KNNImputer, orIterativeImputer. - Missing indicator: a 0/1 column marking which values were filled.
from sklearn.impute import SimpleImputer
imp = SimpleImputer(strategy="median", add_indicator=True)Gradient boosting libraries such as XGBoost and LightGBM handle NaN natively.
What to say in the interview
"I check how much is missing and why: completely random, related to other columns, or related to the value itself. Then I impute with median or mode, add a missing indicator when the absence may be informative, and fit the imputer on the training set only inside a pipeline."
Common mistakes
- Imputing before splitting the data.
- Filling with the mean when a few extreme values distort it.