Multi-column outlier row screen with Tukey fences in pandas

Quick Overview

Implement a pandas screen that flags values outside each numeric column's Tukey fences and returns the rows that are extreme on at least a given number of columns. It tests per-column quantiles, NaN handling, deterministic string output, two-key sorting and empty-result dtypes.

Multi-column outlier row screen with Tukey fences in pandas

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Implement `multi_column_outlier_rows(df, min_cols)`. For every numeric column, flag values strictly outside that column's Tukey inner fences. Return only the rows flagged on at least `min_cols` columns. The function must follow these rules: - Fences are computed per column as `Q1 - 1.5*IQR` and `Q3 + 1.5*IQR`, with quantiles at the default `interpolation='linear'`. A value is flagged only when it is strictly outside a fence, and `NaN` is never a flag. - The output has exactly the columns `['n_flagged_cols', 'flagged_cols']` and keeps the original index labels. - `n_flagged_cols` is `int64`. `flagged_cols` is a single string of the flagged column names in ascending alphabetical order, joined by `', '` (a string, not a list or set). - Rows are sorted by `n_flagged_cols` descending, then by original index ascending. - When no row qualifies, return an empty DataFrame with those two columns and the same dtypes. For example: ```python df = pd.DataFrame({ "a": [10, 12, 40, 11, 13, 12, 10, 45, 11, 14], "b": [5, 6, 5, 7, 30, np.nan, 6, -20, 7, 5], "c": [100, 102, 20, 101, 99, 103, 100, 190, 98, 101], }) multi_column_outlier_rows(df, min_cols=2) ``` ```text n_flagged_cols flagged_cols 7 3 a, b, c 2 2 a, c ``` Row 7 is outside the fences on all three columns and row 2 on `a` and `c`. Row 4 is outside only on `b`, so it falls short of `min_cols=2`, and the missing value in row 5 is not a flag. ```hint Missing values Check what your comparisons return for `NaN`, and for missing values in nullable integer columns, before you count flags per row. ``` ```hint Row order The order has two keys. If you sort twice, check whether the second sort keeps ties in the order the first one left them. ``` ### Constraints & Assumptions - Only numeric columns are screened; other columns are ignored. - `min_cols` is a positive integer. - Column names are strings and index labels are sortable. ### What a Strong Answer Covers - Fences computed separately for each numeric column with linear quantiles, and strict comparisons against them. - Missing values that never count as flags. - A per-row count and an alphabetical name string for each qualifying row, with the original index kept. - The two-key ordering and the empty result, both with the required dtypes. - Time and memory cost in terms of the number of rows and numeric columns. ### Follow-up Questions - Why is a per-column Tukey screen a weak proxy for true multivariate outliers? - How would you make the 1.5 multiplier configurable while keeping results deterministic? - One column is heavily right-skewed, such as order value. How would that affect its flags, and what would you change?

Overview: Implement a pandas screen that flags values outside each numeric column's Tukey fences and returns the rows that are extreme on at least a given number of columns. It tests per-column quantiles, NaN handling, deterministic string output, two-key sorting and empty-result dtypes.

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

Implement multi_column_outlier_rows(df, min_cols). For every numeric column, flag values strictly outside that column's Tukey inner fences. Return only the rows flagged on at least min_cols columns.

The function must follow these rules:

  • Fences are computed per column as Q1 - 1.5*IQR and Q3 + 1.5*IQR , with quantiles at the default interpolation='linear' . A value is flagged only when it is strictly outside a fence, and NaN is never a flag.
  • The output has exactly the columns ['n_flagged_cols', 'flagged_cols'] and keeps the original index labels.
  • n_flagged_cols is int64 . flagged_cols is a single string of the flagged column names in ascending alphabetical order, joined by ', ' (a string, not a list or set).
  • Rows are sorted by n_flagged_cols descending, then by original index ascending.
  • When no row qualifies, return an empty DataFrame with those two columns and the same dtypes.

For example:

df = pd.DataFrame({
    "a": [10, 12, 40, 11, 13, 12, 10, 45, 11, 14],
    "b": [5, 6, 5, 7, 30, np.nan, 6, -20, 7, 5],
    "c": [100, 102, 20, 101, 99, 103, 100, 190, 98, 101],
})
multi_column_outlier_rows(df, min_cols=2)
   n_flagged_cols flagged_cols
7               3      a, b, c
2               2         a, c

Row 7 is outside the fences on all three columns and row 2 on a and c. Row 4 is outside only on b, so it falls short of min_cols=2, and the missing value in row 5 is not a flag.

Constraints & Assumptions

  • Only numeric columns are screened; other columns are ignored.
  • min_cols is a positive integer.
  • Column names are strings and index labels are sortable.

What a Strong Answer Covers Guidance

  • Fences computed separately for each numeric column with linear quantiles, and strict comparisons against them.
  • Missing values that never count as flags.
  • A per-row count and an alphabetical name string for each qualifying row, with the original index kept.
  • The two-key ordering and the empty result, both with the required dtypes.
  • Time and memory cost in terms of the number of rows and numeric columns.

Follow-up Questions Guidance

  • Why is a per-column Tukey screen a weak proxy for true multivariate outliers?
  • How would you make the 1.5 multiplier configurable while keeping results deterministic?
  • One column is heavily right-skewed, such as order value. How would that affect its flags, and what would you change?