Sub topic 11
Why the data is missing
This chapter assumes you can create and index a Series and DataFrame, and that you know isna and fillna from earlier chapters. The code here only inspects data. It does not clean it. The aim is to decide whether a cleaning method is safe before you apply it.
Why the reason matters
dropna and fillna run the same way whatever caused the gaps. Their results are not equally trustworthy. If missing rows differ systematically from complete rows, dropping them or filling them with a mean can bias every statistic you compute afterwards.
Statisticians describe three mechanisms.
| Mechanism | Meaning | Example |
|---|---|---|
| MCAR (missing completely at random) | Missingness is unrelated to any value, observed or not | A sensor loses power at random moments |
| MAR (missing at random) | Missingness depends on other observed columns | Younger respondents skip the income question more often |
| MNAR (missing not at random) | Missingness depends on the missing value itself | High earners leave income blank because it is high |
The name "missing at random" is misleading. MAR means the gaps follow a pattern you can see in the other columns.
Looking for patterns in your data
You cannot prove a mechanism from the data alone. You can look for evidence against MCAR. Build an indicator column that marks missing cells, then compare it with other columns.
import numpy as np
import pandas as pd
df = pd.DataFrame({
"age": [25, 31, np.nan, 45, np.nan, 52, 38, np.nan],
"income": [30, 42, 28, np.nan, np.nan, 55, 47, np.nan],
"group": ["a", "a", "b", "b", "b", "a", "a", "b"],
})
df["income_missing"] = df["income"].isna()
print(df.groupby("group")["income_missing"].mean())
print(df.groupby("income_missing")["age"].mean())The first result shows the share of missing income in each group. Group a has 0.0 and group b has 0.75. All the gaps sit in one group, so the data is not MCAR. This pattern suggests MAR, where group explains the gaps.
The second result compares the mean age of rows with and without income. A large difference is another sign that missingness is linked to other columns.
What each mechanism allows
- MCAR: Dropping rows loses information but does not bias results. Filling with a mean is acceptable, though it shrinks the variance.
- MAR: Dropping rows can bias results. Fill using the columns that explain the gaps, as in the group-wise filling from the previous chapter.
- MNAR: Nothing in the data can repair the bias. Filling with the median of the observed values assumes the missing ones look like them, and here they do not. Your options are to collect more data, to model the missingness explicitly, or to report the limitation.
Using domain knowledge
The data cannot tell you whether the value itself caused a gap, because the missing values are the ones you cannot see. Ask how the data was collected. A blank field in a survey, a failed sensor and a "not applicable" field are different situations, even though pandas shows all three as NaN.
Practice
Exercise
In a customer table, phone is missing mostly for customers whose signup_channel is "web". Which mechanism does this suggest, and which handling approach is more defensible: dropping all rows without a phone, or handling the web customers separately?
Show answer
It suggests MAR, because the missingness depends on an observed column. Dropping all rows without a phone would remove most web customers and bias the data toward other channels. Handling the web customers separately, for example by filling or analysing within signup_channel, is more defensible.
Exercise
Write one line of pandas that gives the share of missing income values for each group, assuming df has those columns.
Show answer
df["income"].isna().groupby(df["group"]).mean()