BasicsPandasMissing ValuesMetaAmazonUber
Problem
Implement clean_missing(df) returning a copy of df where missing (NaN) values in score are filled with the mean of non-null score values, and missing values in city are filled with 'Unknown'.
Why It Matters: Column-specific imputation (numeric mean + categorical sentinel) is one of the first preprocessing steps before training ML models.
Function Signature
import pandas as pd
def clean_missing(df: pd.DataFrame) -> pd.DataFrame:
Sample Input & Output
Example 1: Mixed Numeric & String NaNs
Input:
| score | city |
|---|---|
| 10.0 | NYC |
| NaN | NaN |
| 20.0 | LA |
Expected Output:
| score | city |
|---|---|
| 10.0 | NYC |
| 15.0 | Unknown |
| 20.0 | LA |
Explanation: Mean of [10.0, 20.0] is 15.0, filling row 1's score; row 1's missing city becomes 'Unknown'.
Example 2: No Missing Values
Input:
| score | city |
|---|---|
| 5.0 | SF |
| 7.0 | Boston |
Expected Output:
| score | city |
|---|---|
| 5.0 | SF |
| 7.0 | Boston |
Explanation: Rows without NaNs remain completely unchanged.
Constraints & Edge Cases
- Return a cleaned copy of
df. - Do not mutate the input DataFrame (
out = df.copy()). - Compute
out['score'].mean()ignoringNaNs (Pandas default).
Constraints & Evaluation Criteria
- Return a cleaned copy of `df`.
Hints
Python 3⌘/Ctrl+Enter to Run · ⌘/Ctrl+Shift+Enter to Submit
Loading Code Editor...
+1 Hidden Submit Case
Input / Scenario
score=[10, nan, 20], city=['NYC', None, 'LA']
Expected Output / Criteria
score=[10,15,20], city=['NYC','Unknown','LA']