Fill Missing Values in a pandas Column With Its Median

Quick Overview

Write a pandas function that replaces missing entries in a numeric column with the median of its observed values and returns a new DataFrame. It tests how median handles NaN, avoiding in-place mutation, keeping the float64 dtype, and the trade-offs of median imputation.

Fill Missing Values in a pandas Column With Its Median

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `def fill_median(df: pd.DataFrame) -> pd.DataFrame`. Replace `NaN` entries in the numeric column `'value'` with the median of its non-missing values and return a new DataFrame. Requirements: - Fill `NaN` in `'value'` with the median of its non-missing values. - Leave all other columns unchanged. - Keep the same shape, columns and index. `'value'` stays `float64`. - Do not modify `df` in place. Example: ```python # value = [10.0, NaN, 30.0, NaN, 50.0] -> median of non-missing = 30.0 # result value = [10.0, 30.0, 30.0, 30.0, 50.0] ``` ```hint Two things to check Find out how your median function treats `NaN` by default, and make sure the assignment lands on a frame the caller does not own. ``` ### What a Strong Answer Covers - A median computed over the observed values only, with the `NaN` entries skipped - Work done on a copy, so `df` is unchanged and the other columns, shape and index carry over - `'value'` kept as `float64` after the fill - What happens when every value is missing, and how the median is defined for an even number of observed values ### Follow-up Questions - How would you generalize this to fill every numeric column with its own median? - Why prefer median over mean for imputation here? - What does median imputation do to the column's variance, and how would you let a downstream model know which rows were filled?

Overview: Write a pandas function that replaces missing entries in a numeric column with the median of its observed values and returns a new DataFrame. It tests how median handles NaN, avoiding in-place mutation, keeping the float64 dtype, and the trade-offs of median imputation.

General logo
General
Nov 25, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Implement def fill_median(df: pd.DataFrame) -> pd.DataFrame. Replace NaN entries in the numeric column 'value' with the median of its non-missing values and return a new DataFrame.

Requirements:

  • Fill NaN in 'value' with the median of its non-missing values.
  • Leave all other columns unchanged.
  • Keep the same shape, columns and index. 'value' stays float64 .
  • Do not modify df in place.

Example:

# value = [10.0, NaN, 30.0, NaN, 50.0]  -> median of non-missing = 30.0
# result value = [10.0, 30.0, 30.0, 30.0, 50.0]

What a Strong Answer Covers Guidance

  • A median computed over the observed values only, with the NaN entries skipped
  • Work done on a copy, so df is unchanged and the other columns, shape and index carry over
  • 'value' kept as float64 after the fill
  • What happens when every value is missing, and how the median is defined for an even number of observed values

Follow-up Questions Guidance

  • How would you generalize this to fill every numeric column with its own median?
  • Why prefer median over mean for imputation here?
  • What does median imputation do to the column's variance, and how would you let a downstream model know which rows were filled?