Rank numeric columns by how much Tukey outliers shift their mean
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
Write `outlier_impact_ranking(df)` that, for each numeric column of `df`, flags Tukey outliers and measures how much they shift the column's mean. Return one row per numeric column, ranked by mean shift.
The output must follow these rules:
- Use only numeric columns (`df.select_dtypes`), and exclude nulls from all means and the median.
- Outliers lie strictly outside `[Q1 - 1.5*IQR, Q3 + 1.5*IQR]`, with quartiles at the default `interpolation='linear'`.
- `mean_all` is the mean over all non-null values and `mean_trimmed` is the mean with outliers excluded. Both are reported as computed, without rounding.
- `abs_pct_shift = abs(100 * (mean_all - mean_trimmed) / mean_trimmed)`, rounded to 2 decimal places.
- `robust_gap = abs(mean_all - median_all) / iqr`, rounded to 2 decimal places. It is `NaN` when the IQR is 0, and that case must not raise.
- `distorted` is a bool that is True exactly when `abs_pct_shift > 5.0`.
- Columns are exactly `['column', 'n_outliers', 'mean_all', 'mean_trimmed', 'abs_pct_shift', 'robust_gap', 'distorted']`, sorted by `abs_pct_shift` descending, then `column` ascending, then `reset_index(drop=True)`.
For example, take a DataFrame with numeric columns `a = [1, 2, 3, 4, 100]`, `b = [10, 11, 12, 13, 14]` and `c = [5, 5, 5, 5, NaN]`, plus a text column `name`. The expected result is:
| column | n_outliers | mean_all | mean_trimmed | abs_pct_shift | robust_gap | distorted |
|---|---|---|---|---|---|---|
| a | 1 | 22.0 | 2.5 | 780.0 | 9.5 | True |
| b | 0 | 12.0 | 12.0 | 0.0 | 0.0 | False |
| c | 0 | 5.0 | 5.0 | 0.0 | NaN | False |
In `a`, the value 100 is above the upper fence of 7 and is the only outlier. `b` and `c` tie at 0.0 and are ordered by name. `c` is constant once the null is dropped, so its IQR is 0 and its `robust_gap` is `NaN`. `name` is not numeric and does not appear.
```hint Degenerate columns
Run your function on a constant column and on a column that is entirely null, and look at every division it performs.
```
```hint Ranking ties
Several columns can share the same `abs_pct_shift`, often 0.0. Check that the output order is still fully determined.
```
### What a Strong Answer Covers
- Selecting the numeric columns and dropping nulls per column before any statistic is computed
- Quartiles, fences and the strict outlier test, applied to each column on its own
- The two derived metrics, their rounding, and a `distorted` flag consistent with the reported value
- Guards for a zero or undefined IQR, and what happens when `mean_trimmed` is near 0
- The two-key sort and the exact output columns
### Follow-up Questions
- How would you flag columns where trimming barely moves the mean but `robust_gap` is high?
- Why divide by `mean_trimmed` in `abs_pct_shift`, and what goes wrong when it is close to 0?
- Tukey fences assume a roughly symmetric distribution. What would you change for a heavily right-skewed column such as revenue per user?
Overview: Write a pandas function that scans every numeric column, flags Tukey IQR outliers, and ranks the columns by how far those outliers move the mean. It tests per-column quartile fences, a percentage shift and a skew gap metric, guards for zero IQR, and a two-key sort.