BasicsPandasData CleaningMetaAmazonUber
Problem
Implement drop_incomplete_rows(df, required_cols) that removes any row where any column in required_cols is NaN/None (while keeping rows whose missing values are only in non-required columns), and resets the index.
Why It Matters: Targeted dropna(subset=...) avoids throwing away valid training examples just because an optional metadata column is empty.
Function Signature
import pandas as pd
def drop_incomplete_rows(df: pd.DataFrame, required_cols: list[str]) -> pd.DataFrame:
Sample Input & Output
Example 1: Drop Missing Required Only
Input:
| id | phone | |
|---|---|---|
| 1 | a@x.com | NaN |
| 2 | NaN | 555 |
| 3 | c@x.com | 777 |
Parameter: ['id', 'email'] |
Expected Output:
| id | phone | |
|---|---|---|
| 1 | a@x.com | NaN |
| 3 | c@x.com | 777 |
Explanation: Row 2 is dropped because email is None; Row 1 is KEPT because phone is not in required_cols.
Example 2: All Rows Complete in Subset
Input:
| id | note | |
|---|---|---|
| 1 | a@x.com | NaN |
| 2 | b@x.com | NaN |
Parameter: ['id'] |
Expected Output:
| id | note | |
|---|---|---|
| 1 | a@x.com | NaN |
| 2 | b@x.com | NaN |
Explanation: All rows remain because id has no missing values.
Constraints & Edge Cases
- Keep rows where optional columns are NaN.
- Pass
subset=required_colstodf.dropna(...). - Reset index with
.reset_index(drop=True).
Constraints & Evaluation Criteria
- Keep rows where optional columns are NaN.
Hints
Python 3⌘/Ctrl+Enter to Run · ⌘/Ctrl+Shift+Enter to Submit
Loading Code Editor...
+1 Hidden Submit Case
Input / Scenario
3 rows, required=['email']
Expected Output / Criteria
2 rows kept