Robust vs Classical Spread Report: Compare Std, IQR and MAD per Numeric Column in pandas

Quick Overview

Write a pandas function that reports the standard deviation, IQR and median absolute deviation for every numeric column and flags columns whose spread is inflated by outliers. It tests robust statistics, MAD scaling, multi-key sorting and exact dtype control for a graded DataFrame.

Robust vs Classical Spread Report: Compare Std, IQR and MAD per Numeric Column in pandas

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Write `robust_spread_report(df: pd.DataFrame) -> pd.DataFrame` comparing one classical and two robust spread measures for every numeric column of `df`. For each numeric column `s`: - `std` is `s.std()` with the default `ddof=1`. - `iqr` is `s.quantile(0.75) - s.quantile(0.25)`, using `interpolation='linear'`. - `mad` is the median absolute deviation, `(s - s.median()).abs().median()`. - `robust_ratio` is `std / (1.4826 * mad)`. - `flag` is `'Outlier-inflated spread'` if `robust_ratio > 1.5`, else `'Consistent'`. Return one row per numeric column with the columns exactly `['column', 'std', 'iqr', 'mad', 'robust_ratio', 'flag']`. Sort by `robust_ratio` descending, then `column` ascending, then call `reset_index(drop=True)`. ```hint Which columns count Select the numeric columns with one pandas call before the loop, so text columns never reach the math. ``` ```hint Passing an exact comparison `assert_frame_equal` checks dtypes and the index as well as values. Look at what dtype each output column ends up with after you build the frame. ``` ### Constraints & Assumptions - The test frame guarantees `mad > 0` for every numeric column, so the ratio is always defined. - The output is graded with `assert_frame_equal`. Dtypes must be `object` for `column` and `flag`, and `float64` for `std`, `iqr`, `mad` and `robust_ratio`. - Non-numeric columns do not appear in the report. ### What a Strong Answer Covers - Selecting only the numeric columns and computing each spread measure with the stated settings (`ddof=1`, linear interpolation, median of absolute deviations from the median). - Why multiplying MAD by `1.4826` puts it on the same scale as the standard deviation, so the ratio sits near 1 on clean normal data. - A two-key sort with mixed directions, followed by an index reset. - Output dtypes that survive an exact frame comparison. - How missing values and very short columns affect each statistic. ### Follow-up Questions - Why is `1.4826` the scaling constant for MAD? - How would you handle columns where `mad == 0`? - The IQR can also be turned into an estimate of sigma. What constant would you divide it by, and when would you prefer it over the scaled MAD? - A column comes back with `robust_ratio` around 0.7. What does that say about its distribution?

Overview: Write a pandas function that reports the standard deviation, IQR and median absolute deviation for every numeric column and flags columns whose spread is inflated by outliers. It tests robust statistics, MAD scaling, multi-key sorting and exact dtype control for a graded DataFrame.

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

Write robust_spread_report(df: pd.DataFrame) -> pd.DataFrame comparing one classical and two robust spread measures for every numeric column of df.

For each numeric column s:

  • std is s.std() with the default ddof=1 .
  • iqr is s.quantile(0.75) - s.quantile(0.25) , using interpolation='linear' .
  • mad is the median absolute deviation, (s - s.median()).abs().median() .
  • robust_ratio is std / (1.4826 * mad) .
  • flag is 'Outlier-inflated spread' if robust_ratio > 1.5 , else 'Consistent' .

Return one row per numeric column with the columns exactly ['column', 'std', 'iqr', 'mad', 'robust_ratio', 'flag']. Sort by robust_ratio descending, then column ascending, then call reset_index(drop=True).

Constraints & Assumptions

  • The test frame guarantees mad > 0 for every numeric column, so the ratio is always defined.
  • The output is graded with assert_frame_equal . Dtypes must be object for column and flag , and float64 for std , iqr , mad and robust_ratio .
  • Non-numeric columns do not appear in the report.

What a Strong Answer Covers Guidance

  • Selecting only the numeric columns and computing each spread measure with the stated settings ( ddof=1 , linear interpolation, median of absolute deviations from the median).
  • Why multiplying MAD by 1.4826 puts it on the same scale as the standard deviation, so the ratio sits near 1 on clean normal data.
  • A two-key sort with mixed directions, followed by an index reset.
  • Output dtypes that survive an exact frame comparison.
  • How missing values and very short columns affect each statistic.

Follow-up Questions Guidance

  • Why is 1.4826 the scaling constant for MAD?
  • How would you handle columns where mad == 0 ?
  • The IQR can also be turned into an estimate of sigma. What constant would you divide it by, and when would you prefer it over the scaled MAD?
  • A column comes back with robust_ratio around 0.7. What does that say about its distribution?