Add a Missing-Value Indicator Column to a pandas DataFrame
Quick Overview
Write a pandas function that returns a copy of a DataFrame with a boolean column flagging rows where income is missing, without imputing or changing the original data. It tests missing-value detection, avoiding mutation of the input, and why missingness indicators help later modeling.
Add a Missing-Value Indicator Column to a pandas DataFrame
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Implement `add_missing_indicator(df: pd.DataFrame) -> pd.DataFrame` for a DataFrame with a numeric column `'income'`. Return a copy of `df` with a boolean column `'income_missing'` flagging rows with missing income.
The function must meet these requirements:
- `'income_missing'` equals `df['income'].isna()` (`True` where the value is NaN, `False` otherwise).
- The new column is the last column.
- `'income'` and its NaNs stay unchanged. Do not fill or impute.
- The original index and row order are preserved.
- The input `df` is not mutated.
Example:
```python
df = pd.DataFrame({'income': [50000.0, None, 72000.0]})
out = add_missing_indicator(df)
# income income_missing
# 0 50000.0 False
# 1 NaN True
# 2 72000.0 False
```
```hint Keep the caller's frame intact
Decide which object the new column is assigned to. Adding a column to `df` itself would break one of the requirements.
```
### What a Strong Answer Covers
- How the missing mask is computed and which values it treats as missing.
- How the answer avoids mutating the input DataFrame.
- Why `income` keeps its NaNs and why the new column ends up last.
- Preservation of the index and row order.
### Follow-up Questions
- How would you generalize this to flag missing values across multiple columns?
- Why keep the missing indicator instead of just imputing the NaNs?
- Some rows record unreported income as `-1` instead of NaN. How does that change the function?
Overview: Write a pandas function that returns a copy of a DataFrame with a boolean column flagging rows where income is missing, without imputing or changing the original data. It tests missing-value detection, avoiding mutation of the input, and why missingness indicators help later modeling.
Implement add_missing_indicator(df: pd.DataFrame) -> pd.DataFrame for a DataFrame with a numeric column 'income'. Return a copy of df with a boolean column 'income_missing' flagging rows with missing income.
The function must meet these requirements:
'income_missing'
equals
df['income'].isna()
(
True
where the value is NaN,
False
otherwise).
The new column is the last column.
'income'
and its NaNs stay unchanged. Do not fill or impute.
The original index and row order are preserved.
The input
df
is not mutated.
Example:
df = pd.DataFrame({'income': [50000.0, None, 72000.0]})
out = add_missing_indicator(df)
# income income_missing
# 0 50000.0 False
# 1 NaN True
# 2 72000.0 False
What a Strong Answer Covers Guidance
How the missing mask is computed and which values it treats as missing.
How the answer avoids mutating the input DataFrame.
Why
income
keeps its NaNs and why the new column ends up last.
Preservation of the index and row order.
Follow-up Questions Guidance
How would you generalize this to flag missing values across multiple columns?
Why keep the missing indicator instead of just imputing the NaNs?
Some rows record unreported income as
-1
instead of NaN. How does that change the function?