Remove IQR Outliers From a Column

Quick Overview

Write a pandas function that drops the rows of a DataFrame whose value in one column lies outside the inclusive 1.5×IQR fences. It tests quantile calculation, boolean masking, index resetting and dtype preservation for an exact frame comparison, plus how missing values are handled.

Remove IQR Outliers From a Column

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `remove_iqr_outliers(df: pd.DataFrame, col: str) -> pd.DataFrame` returning a new DataFrame with IQR-outlier rows of `col` removed. Keep rows where `Q1 - 1.5*IQR <= value <= Q3 + 1.5*IQR`. Requirements: - Compute `Q1` and `Q3` with `quantile(0.25)` and `quantile(0.75)` using `interpolation='linear'`, and set `IQR = Q3 - Q1`. - Fences are inclusive on both bounds. - Preserve all columns and do not mutate the input. - Call `.reset_index(drop=True)` so the index is `0..n-1`. The result is graded with `pandas.testing.assert_frame_equal`, so the index and dtypes must match. - Assume `col` is numeric and present in the frame. Example: ```python df = pd.DataFrame({"x": [10, 12, 11, 13, 100]}) remove_iqr_outliers(df, "x") # x # 0 10 # 1 12 # 2 11 # 3 13 # (100 is removed as an upper outlier) ``` ```hint What the grader compares The comparison checks the index and dtypes as well as the values. Think about which index labels the surviving rows carry after filtering, and whether a value sitting exactly on a fence stays. ``` ### What a Strong Answer Covers - Quartiles from `quantile(0.25)` and `quantile(0.75)` with linear interpolation, and both fences derived from the IQR. - A keep mask that is inclusive on both fences. - A new frame with every column and the original dtypes, leaving the input untouched. - Resetting the index to `0..n-1`. - What happens to rows where `col` is NaN. ### Follow-up Questions - How would you support removing outliers across multiple columns at once? - Why use inclusive fences, and when might that matter? - How would you remove outliers within each group of a categorical column instead of across the whole column? - When would you cap outliers at the fences instead of dropping the rows?

Overview: Write a pandas function that drops the rows of a DataFrame whose value in one column lies outside the inclusive 1.5×IQR fences. It tests quantile calculation, boolean masking, index resetting and dtype preservation for an exact frame comparison, plus how missing values are handled.

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

Implement remove_iqr_outliers(df: pd.DataFrame, col: str) -> pd.DataFrame returning a new DataFrame with IQR-outlier rows of col removed. Keep rows where Q1 - 1.5*IQR <= value <= Q3 + 1.5*IQR.

Requirements:

  • Compute Q1 and Q3 with quantile(0.25) and quantile(0.75) using interpolation='linear' , and set IQR = Q3 - Q1 .
  • Fences are inclusive on both bounds.
  • Preserve all columns and do not mutate the input.
  • Call .reset_index(drop=True) so the index is 0..n-1 . The result is graded with pandas.testing.assert_frame_equal , so the index and dtypes must match.
  • Assume col is numeric and present in the frame.

Example:

df = pd.DataFrame({"x": [10, 12, 11, 13, 100]})
remove_iqr_outliers(df, "x")
#     x
# 0  10
# 1  12
# 2  11
# 3  13
# (100 is removed as an upper outlier)

What a Strong Answer Covers Guidance

  • Quartiles from quantile(0.25) and quantile(0.75) with linear interpolation, and both fences derived from the IQR.
  • A keep mask that is inclusive on both fences.
  • A new frame with every column and the original dtypes, leaving the input untouched.
  • Resetting the index to 0..n-1 .
  • What happens to rows where col is NaN.

Follow-up Questions Guidance

  • How would you support removing outliers across multiple columns at once?
  • Why use inclusive fences, and when might that matter?
  • How would you remove outliers within each group of a categorical column instead of across the whole column?
  • When would you cap outliers at the fences instead of dropping the rows?