Python · medium

Detect and remove outliers using the IQR method in pandas.

Asked in Data Scientist interviews, in the Python round.

Short answer

Compute Q1,Q3,IQR; keep rows within [Q1-1.5*IQR, Q3+1.5*IQR].

How to answer it

The interquartile range fence: anything below Q1 minus 1.5 IQR or above Q3 plus 1.5 IQR is flagged.

q1, q3 = df["amount"].quantile([0.25, 0.75])
iqr = q3 - q1
lo, hi = q1 - 1.5 * iqr, q3 + 1.5 * iqr
flag = (df["amount"] < lo) | (df["amount"] > hi)
clean = df[~flag]
print(f"{flag.mean():.1%} of rows flagged, {flag.sum()} rows")

Two things to say before running it. "Remove" is a decision, not a default: an outlier in a fraud dataset is the thing you are looking for, and an outlier in revenue may be the enterprise customer who pays the bills. Cap (winsorise) or model robustly before you delete. And the fence is per group where groups differ: order sizes for wholesale and retail customers need separate fences, so compute the quantiles inside a groupby and transform.

For a skewed metric, apply the rule on the log scale, or the upper fence flags a third of the data. Always report how many rows the rule removed; a cleaning step that silently drops 15% is a bug in disguise.

Related questions

Practice this for real