Drop Rows With Missing Values From a Pandas DataFrame
Quick Overview
Write a pandas function that removes every row containing a missing value, returns a fresh 0-based index, and leaves the input DataFrame unchanged. It tests dropna semantics, what pandas counts as missing, index handling and avoiding mutation of the caller's data.
Drop Rows With Missing Values From a Pandas DataFrame
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Implement `drop_missing_rows(df: pd.DataFrame) -> pd.DataFrame` that returns a copy of `df` with every row containing at least one missing value removed (`how='any'`).
Requirements:
- Reset the index via `reset_index(drop=True)` to a 0..n-1 `RangeIndex`.
- Keep columns and their order unchanged.
- Dtypes may stay float where NaN was present (no explicit casting required).
- Do not mutate `df`.
For example:
```python
df = pd.DataFrame({"a": [1, None, 3], "b": [4, 5, None]})
drop_missing_rows(df)
# a b
# 0 1.0 4.0
```
```hint Built-ins and labels
pandas already has a method for removing rows with missing values. After you call it, look at the row labels that are left and compare them with the index the contract asks for.
```
### What a Strong Answer Covers
- The pandas call that removes rows with any missing value, and why it leaves `df` untouched.
- Which values pandas treats as missing, and which values only look missing (empty strings, the text "NaN").
- Why the index is reset, and what `drop=True` changes about the output columns.
- What happens to dtypes, and what the result looks like when no row survives or when the input is empty.
### Follow-up Questions
- How would you drop a row only when all of its values are missing, or only when certain columns are missing?
- How would you keep rows that have at least k non-missing values?
- Every surviving value in a column that once held NaN is a whole number. How do you get an integer dtype back?
- The data is a CSV file too large to load into memory. How do you apply the same filter?
Overview: Write a pandas function that removes every row containing a missing value, returns a fresh 0-based index, and leaves the input DataFrame unchanged. It tests dropna semantics, what pandas counts as missing, index handling and avoiding mutation of the caller's data.
Implement drop_missing_rows(df: pd.DataFrame) -> pd.DataFrame that returns a copy of df with every row containing at least one missing value removed (how='any').
Requirements:
Reset the index via
reset_index(drop=True)
to a 0..n-1
RangeIndex
.
Keep columns and their order unchanged.
Dtypes may stay float where NaN was present (no explicit casting required).
Do not mutate
df
.
For example:
df = pd.DataFrame({"a": [1, None, 3], "b": [4, 5, None]})
drop_missing_rows(df)
# a b
# 0 1.0 4.0
What a Strong Answer Covers Guidance
The pandas call that removes rows with any missing value, and why it leaves
df
untouched.
Which values pandas treats as missing, and which values only look missing (empty strings, the text "NaN").
Why the index is reset, and what
drop=True
changes about the output columns.
What happens to dtypes, and what the result looks like when no row survives or when the input is empty.
Follow-up Questions Guidance
How would you drop a row only when all of its values are missing, or only when certain columns are missing?
How would you keep rows that have at least k non-missing values?
Every surviving value in a column that once held NaN is a whole number. How do you get an integer dtype back?
The data is a CSV file too large to load into memory. How do you apply the same filter?