Per-Group IQR Outlier Removal

Quick Overview

Implement a pandas function that removes outliers using Tukey IQR fences computed separately within each group, keeping all columns and the original row order. It tests groupby transform, aligning per-row bounds, inclusive filtering, and edge cases such as small groups and NaN values.

Per-Group IQR Outlier Removal

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Implement `remove_group_iqr_outliers(df: pd.DataFrame, group_col: str, value_col: str) -> pd.DataFrame`, which drops outliers using the IQR rule computed independently within each group of `group_col`. Bounds from one group must never affect another. Requirements: - Per group, compute `Q1`/`Q3` of `value_col` via `quantile(0.25)`/`quantile(0.75)` with `interpolation='linear'`, and `IQR = Q3 - Q1`. - Keep only rows whose value lies in that group's `[Q1 - 1.5*IQR, Q3 + 1.5*IQR]` (inclusive). - Preserve all columns and the original row order. - Return the surviving rows with `.reset_index(drop=True)`. Example: ```python df = pd.DataFrame({ "g": ["a", "a", "a", "a", "b", "b", "b"], "v": [10, 11, 12, 100, 5, 6, 500], }) remove_group_iqr_outliers(df, "g", "v") # g v # 0 a 10 # 1 a 11 # 2 a 12 # 3 b 5 # 4 b 6 # 5 b 500 # (100 is dropped; 500 stays because group b's own fences are -365.75 and 624.25) ``` ```hint One bound per row Look for a groupby operation that returns one value per original row instead of one per group. With per-row bounds you can build a single mask over the whole frame and filter once, which also keeps the input order. ``` ```hint Check the small group Compute group b's quartiles by hand before trusting your intuition about 500. With only three rows, the extreme value itself pulls Q3 a long way. ``` ### What a Strong Answer Covers - Computes Q1, Q3 and the fences separately for each group with linear interpolation, so one group's data never moves another group's bounds. - Aligns every row with its own group's bounds and filters with an inclusive mask. - Keeps all columns and the original row order, then resets the index. - Verifies the example by hand, including why 500 survives in group b. - Discusses NaN values, missing group keys and very small groups. ### Follow-up Questions - How would you keep this vectorized as the number of groups grows large? - How should NaN values in `value_col` be handled? - Groups with only a handful of rows give unstable quartiles. How would you treat them? - How would the result change if the fences were computed on the whole column instead of per group?

Overview: Implement a pandas function that removes outliers using Tukey IQR fences computed separately within each group, keeping all columns and the original row order. It tests groupby transform, aligning per-row bounds, inclusive filtering, and edge cases such as small groups and NaN values.

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

Implement remove_group_iqr_outliers(df: pd.DataFrame, group_col: str, value_col: str) -> pd.DataFrame, which drops outliers using the IQR rule computed independently within each group of group_col. Bounds from one group must never affect another.

Requirements:

  • Per group, compute Q1 / Q3 of value_col via quantile(0.25) / quantile(0.75) with interpolation='linear' , and IQR = Q3 - Q1 .
  • Keep only rows whose value lies in that group's [Q1 - 1.5*IQR, Q3 + 1.5*IQR] (inclusive).
  • Preserve all columns and the original row order.
  • Return the surviving rows with .reset_index(drop=True) .

Example:

df = pd.DataFrame({
    "g": ["a", "a", "a", "a", "b", "b", "b"],
    "v": [10, 11, 12, 100, 5, 6, 500],
})
remove_group_iqr_outliers(df, "g", "v")
#    g    v
# 0  a   10
# 1  a   11
# 2  a   12
# 3  b    5
# 4  b    6
# 5  b  500
# (100 is dropped; 500 stays because group b's own fences are -365.75 and 624.25)

What a Strong Answer Covers Guidance

  • Computes Q1, Q3 and the fences separately for each group with linear interpolation, so one group's data never moves another group's bounds.
  • Aligns every row with its own group's bounds and filters with an inclusive mask.
  • Keeps all columns and the original row order, then resets the index.
  • Verifies the example by hand, including why 500 survives in group b.
  • Discusses NaN values, missing group keys and very small groups.

Follow-up Questions Guidance

  • How would you keep this vectorized as the number of groups grows large?
  • How should NaN values in value_col be handled?
  • Groups with only a handful of rows give unstable quartiles. How would you treat them?
  • How would the result change if the fences were computed on the whole column instead of per group?