Detect Outliers with the IQR Method
Flag statistical outliers using the interquartile range before they distort aggregates and model training.
Task
Compute IQR bounds and flag rows outside them for review.
When to use this
A handful of extreme values skew averages and confuse dashboards that should reflect typical behaviour.
Prerequisites
- pandas or an equivalent dataframe library
- A numeric column to analyse
- A policy for handling outliers
Steps
1. Compute IQR bounds and flag outliers.
import pandas as pd
q1, q3 = df['latency'].quantile([0.25, 0.75])
iqr = q3 - q1
low, high = q1 - 1.5 * iqr, q3 + 1.5 * iqr
df['outlier'] = (df['latency'] < low) | (df['latency'] > high)Expected output: Rows outside 1.5 times the IQR are flagged in a boolean column.
2. Report the count and bounds.
print(df['outlier'].sum(), low, high)Expected output: The count and thresholds tell you how much data is affected before you act.
Troubleshooting
Problem
Too many flagged in skewed data
Solution
Use a looser multiplier or a robust method such as median absolute deviation.
Problem
Outliers are legitimate
Solution
Review flagged rows before deleting; outliers may be the signal you care about.
Limitations
- Assumes a roughly symmetric distribution.
- Flags rather than removes automatically.
- Does not handle multivariate outliers.
What this recipe does
Compute IQR bounds and flag rows outside them for review.
A handful of extreme values skew averages and confuse dashboards that should reflect typical behaviour.
Prerequisites
- pandas or an equivalent dataframe library
- A numeric column to analyse
- A policy for handling outliers
Procedure
- Compute IQR bounds and flag outliers.
import pandas as pd
q1, q3 = df['latency'].quantile([0.25, 0.75])
iqr = q3 - q1
low, high = q1 - 1.5 * iqr, q3 + 1.5 * iqr
df['outlier'] = (df['latency'] < low) | (df['latency'] > high)
Expected result: Rows outside 1.5 times the IQR are flagged in a boolean column.
- Report the count and bounds.
print(df['outlier'].sum(), low, high)
Expected result: The count and thresholds tell you how much data is affected before you act.
How to confirm it worked
Run the commands above and confirm every expected result in order. If a step does not produce the stated output, use the troubleshooting notes before continuing. A passing run shows the flagged count and bounds are reported, and reviewing a sample confirms the flags are plausible extremes.
Troubleshooting
Too many flagged in skewed data — Use a looser multiplier or a robust method such as median absolute deviation.
Outliers are legitimate — Review flagged rows before deleting; outliers may be the signal you care about.
Limitations
- Assumes a roughly symmetric distribution.
- Flags rather than removes automatically.
- Does not handle multivariate outliers.