Compare Z-Score and IQR Outlier Counts per Column and Rank the Gap with Skew in Pandas
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
Write `zscore_vs_iqr_gap(df)` that, for each numeric column, counts the points flagged by the IQR rule and by the z-score rule and reports the gap between the two counts alongside the column's skew. Return one row per numeric column.
The result must meet these requirements:
- `n_iqr`: the number of values strictly below `Q1 - 1.5*IQR` or strictly above `Q3 + 1.5*IQR`, with quantiles computed using `interpolation='linear'`. A value equal to a fence is not an outlier.
- `n_zscore`: the number of values with `abs((x - mean) / std) > 3.0`, using the population standard deviation `s.std(ddof=0)`.
- `skew`: pandas `Series.skew()`, which is bias-corrected (the `scipy.stats.skew` default is not).
- Columns exactly `['column', 'skew', 'n_iqr', 'n_zscore', 'gap']`, with `gap = n_iqr - n_zscore` as `int64`.
- Rows sorted by `gap` descending, then `column` ascending, followed by `reset_index(drop=True)`.
Example input with 12 rows (`region` is a string column):
```text
revenue: 0 0 0 0 0 0 0 0 0 1 1 10
tenure: 1 2 3 4 5 6 7 8 9 10 11 12
plan_price: 5 5 5 5 5 5 5 5 5 5 5 5
region: east, west, ... (strings)
```
Expected result (skew rounded to six decimals):
```text
column skew n_iqr n_zscore gap
0 revenue 3.356188 3 1 2
1 plan_price 0.000000 0 0 0
2 tenure 0.000000 0 0 0
```
For `revenue`, Q1 is 0 and Q3 is 0.25, so the upper fence is 0.625 and the values 1, 1 and 10 are IQR outliers. The mean is 1 and the population standard deviation is the square root of 7.5, so only 10 has a z-score above 3 (about 3.29). `plan_price` and `tenure` both have a gap of 0, so they are ordered by name. `region` is not numeric and gets no row.
```hint Pinned conventions
Each count depends on a convention: the degrees of freedom of the standard deviation, strict versus inclusive comparisons at the fences, and which skew estimator. Check each pandas default against the requirements.
```
```hint Degenerate columns
Work out what each statistic returns for a column with some NaNs, a constant column, and a column that is entirely NaN.
```
### Constraints & Assumptions
- Only the columns that `df.select_dtypes(include='number')` returns produce rows. Other columns are ignored.
- Missing values are ignored within each column.
- Column names are strings, so sorting by `column` is well defined.
### What a Strong Answer Covers
- Selecting the numeric columns and computing the Tukey fences from linear-interpolated quartiles with strict comparisons.
- Computing z-scores with the population standard deviation and a strict `> 3.0` threshold.
- Using the bias-corrected pandas skew and explaining why the estimator choice changes the number.
- Meeting the output contract: exact columns, int64 counts, the two-key sort and a reset index.
- Handling NaNs, constant columns and all-NaN columns, and explaining why skewed columns get a large positive gap.
### Follow-up Questions
- Why does high skew widen the gap toward IQR flagging more points?
- How would you handle columns with `std == 0`, or columns that are entirely NaN?
- For which sample sizes can the z-score rule never flag a point at all?
Overview: Write a pandas function that counts outliers in every numeric column with both the Tukey IQR fences and a z-score threshold of 3, then ranks columns by the gap alongside their skew. It tests exact statistical conventions, edge cases like constant columns, and why skewed data makes the two rules disagree.