Sign in to track progress, unlock prep layers, and browse your pipeline.Sign in
Fill missing values with means
Problem Description
You are provided with a pandas DataFrame that contains missing values (NaN) across several numeric columns.
Task
- Core Implementation: Write a concise pandas expression to replace all NaN values in each numeric column with that column's respective mean. Non-numeric columns must remain entirely unchanged.
- Advanced Considerations: Explain how you would extend this solution to address the following scenarios:
- (a) In-place Modification: Perform the replacement operation directly on the original DataFrame without creating a new one.
- (b) Group-based Imputation: Compute and apply means for missing values based on specific groups defined by a designated key column (e.g., a 'group' column).
- (c) Handling All-NaN Columns: Gracefully skip or handle columns where all values are NaN, ensuring the operation does not fail or produce undesirable results (e.g., filling with NaN).
AI interview prep
Core dilemma
Ensuring correct, efficient, and robust imputation of missing values in a DataFrame while preserving data types and gracefully handling edge cases like all-NaN columns or complex grouping logic.
Mid-level signals3
Senior signals3
Red flags3
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Fill missing values with means
Problem Description
You are provided with a pandas DataFrame that contains missing values (NaN) across several numeric columns.
Task
- Core Implementation: Write a concise pandas expression to replace all NaN values in each numeric column with that column's respective mean. Non-numeric columns must remain entirely unchanged.
- Advanced Considerations: Explain how you would extend this solution to address the following scenarios:
- (a) In-place Modification: Perform the replacement operation directly on the original DataFrame without creating a new one.
- (b) Group-based Imputation: Compute and apply means for missing values based on specific groups defined by a designated key column (e.g., a 'group' column).
- (c) Handling All-NaN Columns: Gracefully skip or handle columns where all values are NaN, ensuring the operation does not fail or produce undesirable results (e.g., filling with NaN).