Handling Missing Values: Drop, Impute or Flag
Compare deleting rows, mean and median imputation, group-wise fills, forward fill and missing flags, and see how each choice changes your averages.
Welcome back!
Step 1 of 11Learn
How dropping rows quietly eats your data
The simplest way to deal with missing values is to drop every row that has one. Most tools make this a single command, df.dropna() in pandas. It is called listwise deletion or complete-case analysis, and it is fine when only a few rows are affected and the data is plausibly MCAR.
The trouble is how fast it adds up. Imagine a table with 10 columns, each missing 5% of its values, independently of the others. A row survives only if all ten values are present. The chance of that is
0.9510 ≈ 0.60
So deleting incomplete rows throws away about 40% of your data, even though each column looks 95% complete. With 20 such columns you would keep only about 36%.
Fewer rows mean wider confidence intervals and weaker tests. Worse, if the data is not MCAR, the surviving rows are a biased subset. In the employee survey from the last lesson, complete cases would be mostly the happy employees.
A gentler option is pairwise deletion: each calculation uses all rows that have the values it needs. The correlation of age and income uses rows with both present, even if region is missing. It keeps more data but means different results are based on slightly different subsets.
Step 2 of 11Try the calculator
How many complete rows survive?
Assume each column is missing the same share of values, independently. See what fraction of rows has no missing value at all.
- Share of complete rows
- 59.9 %
- Complete rows kept
- 29,937
- Rows lost by listwise deletion
- 20,063
Step 3 of 11Practice · Work it out
A table has 8 columns, each missing 3% of values independently. About what percentage of rows are complete? Round to one decimal place.
Step 4 of 11Learn
The imputation toolbox
Imputation means filling missing values with estimates. The common options, from simplest to more careful:
- Mean imputation: replace blanks with the column average. Easy, but sensitive to outliers.
- Median imputation: use the middle value instead. Better for skewed columns like income, prices or time on site, where a few large values would inflate the mean.
- Mode imputation: use the most common category. Used for categorical columns such as payment method, though it can overstate the most common group.
- Group-wise imputation: fill with the median of a relevant group, such as the median salary for the same job title and city. This uses what you know (the MAR idea) and is usually much more realistic than one global value.
- Forward fill and interpolation: for time series, carry the last known value forward or draw a straight line between neighbors. A thermostat reading missing at 14:00 is best estimated from 13:00 and 15:00, not from the yearly average.
- Model-based imputation: predict the missing value from other columns with regression or nearest neighbors; multiple imputation repeats this several times to reflect uncertainty.
Sometimes the right answer is not to impute at all. If a column is 70% missing, it may be better to drop the column or to analyze only the subset where it was collected.
Step 5 of 11Compare
Choosing a strategy
| Aspect | Drop rows | Simple fill (mean/median/mode) | Group-wise or model fill | Flag + fill |
|---|---|---|---|---|
| Best when | Few rows affected and plausibly MCAR | Quick analysis, low missingness, distribution roughly symmetric (mean) or skewed (median) | Missingness depends on recorded variables (MAR) | Missingness itself may carry information |
| Main risk | Bias and lost sample size | Shrinks variance and weakens relationships | More work; model errors carried into data | Extra columns to explain |
| Effect on averages | Can shift if missing rows differ | Mean fill keeps the mean; median fill moves it toward the median | Usually closest to truth if the model is sensible | Same as the fill used |
| Typical example | Ten corrupted rows out of 80,000 | Median delivery time for a handful of blanks | Median salary by job title and city | income_missing = 1 used as a model feature |
Step 6 of 11Learn
The hidden cost of mean imputation
Mean imputation looks harmless because it leaves the column mean unchanged. The damage is to the spread.
Take five survey answers for weekly exercise hours, two of them missing: 4, 8, 12, blank, blank. The three known values have mean 8 and sample standard deviation 4. Now fill the blanks with 8: the data becomes 4, 8, 12, 8, 8. The mean is still 8, but the squared deviations are 16, 0, 16, 0, 0, which sum to 32. Divided by n − 1 = 4, the variance is 8 and the standard deviation drops to about 2.83.
The data now looks more consistent than reality, because you added two values that sit exactly at the center. Any later calculation that relies on spread, such as confidence intervals, t-tests or correlations, becomes overconfident. Correlations also weaken: the filled rows sit at the mean of one variable no matter what the other variable says, which flattens the relationship.
That doesn't make mean imputation forbidden. With 1% missing it hardly matters. With 20% missing, it can change conclusions. The fix is to use more realistic methods, to impute within groups, or at least to report how many values were imputed so readers can judge.
Step 7 of 11Practice · Work it out
Daily step counts for five days are 6,000, 9,000, 12,000, blank, blank. After filling both blanks with the mean of the known values, what is the sample standard deviation of the five values? Round to the nearest whole step.
Step 8 of 11Practice · Code challenge · Python
Write impute_median(values) that returns a new list in which every None is replaced by the median of the non-missing values. If every value is None, return the list unchanged. You may use statistics.median.
impute_median([3, None, 1, 10])impute_median([None, 2, 4])impute_median([None, None])impute_median([5, 7])
Step 9 of 11Note
Step 10 of 11Practice · What would you do?
What would you do?
A building sensor records temperature every hour. Over a year, 3% of hourly readings are missing, mostly in short gaps of one to three hours caused by Wi-Fi drops. You need complete hourly data for an energy model. Which approach fits best?
Step 11 of 11Practice · Choose one
Customer income is strongly right-skewed: most customers earn $30,000–$90,000, and a few earn several million. 4% of values are missing. If you choose a single-value fill, which is usually the better choice?
Lesson complete!