Group-Wise Median Imputation in pandas

Quick Overview

Write a pandas function that fills missing numeric values with the median of each row's group while keeping the input frame, index order and float dtype intact. It tests groupby with transform, index alignment, NaN handling, and edge cases such as groups with no observed values.

Group-Wise Median Imputation in pandas

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Implement `fill_group_median(df)` for a pandas DataFrame with a categorical column `'group'` and a numeric column `'value'` that contains NaNs. Replace each missing `'value'` with the median of `'value'` within its own group. The function must: - Fill only NaN entries, each with its group's median. - Leave `'group'` unchanged and keep `'value'` as `float64`. - Return a new DataFrame with the same shape, columns, and index order. - Not mutate `df`. For example: ```python df = pd.DataFrame({ 'group': ['a', 'a', 'a', 'b', 'b'], 'value': [1.0, 3.0, np.nan, 10.0, np.nan], }) fill_group_median(df)['value'].tolist() # [1.0, 3.0, 2.0, 10.0, 10.0] # a-median=2.0, b-median=10.0 ``` ```hint Per-row group statistic Look for a grouped operation that returns one value per original row, aligned to the original index, so the result can go straight into fillna. ``` ```hint Edge cases Before you finish, check what your code does for a group with no observed values and for a row whose group key is missing. ``` ### What a Strong Answer Covers - Computing each row's group median aligned to the original index, so the fill values line up row by row. - Filling only the missing positions while leaving observed values and the `'group'` column untouched. - Working on a copy so `df` is never mutated, and returning the same shape, columns, index order and `float64` dtype. - How pandas' median treats NaNs inside a group, and what happens to a group with no observed values. - Time and space complexity of the grouped computation. ### Follow-up Questions - What happens if an entire group is NaN, and how would you handle it? - Why use `transform` instead of `apply` or a merge with a per-group aggregate? - How should rows whose `'group'` value is itself missing be handled? - How would you extend the function to impute several numeric columns at once, or to group by more than one key?

Overview: Write a pandas function that fills missing numeric values with the median of each row's group while keeping the input frame, index order and float dtype intact. It tests groupby with transform, index alignment, NaN handling, and edge cases such as groups with no observed values.

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

Implement fill_group_median(df) for a pandas DataFrame with a categorical column 'group' and a numeric column 'value' that contains NaNs. Replace each missing 'value' with the median of 'value' within its own group.

The function must:

  • Fill only NaN entries, each with its group's median.
  • Leave 'group' unchanged and keep 'value' as float64 .
  • Return a new DataFrame with the same shape, columns, and index order.
  • Not mutate df .

For example:

df = pd.DataFrame({
    'group': ['a', 'a', 'a', 'b', 'b'],
    'value': [1.0, 3.0, np.nan, 10.0, np.nan],
})
fill_group_median(df)['value'].tolist()
# [1.0, 3.0, 2.0, 10.0, 10.0]  # a-median=2.0, b-median=10.0

What a Strong Answer Covers Guidance

  • Computing each row's group median aligned to the original index, so the fill values line up row by row.
  • Filling only the missing positions while leaving observed values and the 'group' column untouched.
  • Working on a copy so df is never mutated, and returning the same shape, columns, index order and float64 dtype.
  • How pandas' median treats NaNs inside a group, and what happens to a group with no observed values.
  • Time and space complexity of the grouped computation.

Follow-up Questions Guidance

  • What happens if an entire group is NaN, and how would you handle it?
  • Why use transform instead of apply or a merge with a per-group aggregate?
  • How should rows whose 'group' value is itself missing be handled?
  • How would you extend the function to impute several numeric columns at once, or to group by more than one key?