Python Central HubSign in

Data Analytics with Python / Phase 4: Data Preprocessing and Cleaning

Outlier Detection (IQR Method)

Detect outliers using the Interquartile Range (IQR) rule, compute bounds, and flag extreme values.

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

Python
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)

Python
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.

Visualize it

diagram IQR outlier detection mermaid
Values are sorted, quartiles are computed, and anything outside the fences is flagged as an outlier.

A box plot is just this rule drawn as a picture: the box spans Q1 to Q3, the whiskers reach out to the fences, and anything past them is plotted as its own dot.

sketch Boxplot with IQR fences p5.js
The box covers Q1-Q3, whiskers extend to the fences, and points beyond the fences are outliers.

🧪 Try It Yourself

Exercise 1 – Compute Q1, Q3, and IQR

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 2 – Compute the fences

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 3 – Flag the outliers

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Check your understanding

quizCheck yourself
  1. How is the IQR defined?

    Show answer

    A — Q3 minus Q1 — IQR is the 75th percentile minus the 25th percentile.

  2. Which bounds flag outliers in the common IQR rule?

    Show answer

    A — Q1 - 1.5 x IQR and Q3 + 1.5 x IQR — Values below the lower fence or above the upper fence are flagged.

  3. For amounts 100, 110, 120, 115, 105, 130, 125 and 3000, which value falls outside the fences?

    Show answer

    A — 3000 only — Q1 is 108.75 and Q3 is 126.25, so the IQR is 17.5 and the fences are 82.5 and 152.5. Only 3000 lies outside.

  4. What does the lesson say about outlier detection versus removal?

    Show answer

    A — Detection is not removal, so validate outliers with domain context — The lesson flags outliers in a column and keeps the rows. It also notes that IQR is robust for skewed data.

Next

Detection is only half the job — see Handling Outliers for what to actually do once you've found them.

Was this page helpful?

Let us know how we did