Outlier Detection (IQR Method)
What is IQR?
The Interquartile Range (IQR) is:
- Q3 (75th percentile) − Q1 (25th percentile)
A common rule flags outliers outside:
- Lower bound = Q1 − 1.5 × IQR
- Upper bound = Q3 + 1.5 × IQR
Example
IQR bounds
import pandas as pd
df = pd.DataFrame({"amount": [100, 110, 120, 115, 105, 130, 125, 3000]})
q1 = df["amount"].quantile(0.25)
q3 = df["amount"].quantile(0.75)
iqr = q3 - q1
lower = q1 - 1.5 * iqr
upper = q3 + 1.5 * iqr
print("Q1:", q1)
print("Q3:", q3)
print("IQR:", iqr)
print("Bounds:", lower, upper)
outliers = df[(df["amount"] < lower) | (df["amount"] > upper)]
print(outliers)IQR bounds
import pandas as pd
df = pd.DataFrame({"amount": [100, 110, 120, 115, 105, 130, 125, 3000]})
q1 = df["amount"].quantile(0.25)
q3 = df["amount"].quantile(0.75)
iqr = q3 - q1
lower = q1 - 1.5 * iqr
upper = q3 + 1.5 * iqr
print("Q1:", q1)
print("Q3:", q3)
print("IQR:", iqr)
print("Bounds:", lower, upper)
outliers = df[(df["amount"] < lower) | (df["amount"] > upper)]
print(outliers)Flag outliers (keep rows, add a column)
Outlier flag
df["is_outlier"] = (df["amount"] < lower) | (df["amount"] > upper)
print(df)Outlier flag
df["is_outlier"] = (df["amount"] < lower) | (df["amount"] > upper)
print(df)Notes
- IQR is robust for skewed data.
- Outlier detection is not outlier removal.
- Always validate outliers with domain context.
If this helped you, consider buying me a coffee ☕
Buy me a coffeeWas this page helpful?
Let us know how we did
