Skip to content
Hands-on Mentor
ENTR
Back to blog

How to find outliers with the IQR method in Python

An outlier is a value far from the rest of the data. Learn how to find outliers with the IQR rule, read a box plot, and decide whether to keep or remove them.

5 min read

To find outliers, the most common method is the IQR (interquartile range) rule: a value is an outlier if it is more than 1.5 times the IQR below the first quartile or above the third quartile. A box plot draws the same rule as a picture. Finding an outlier is easy. The hard part is deciding what to do with it.

What is an outlier?

An outlier is an observation that sits far away from the rest of the data. It can be a mistake, a rare but real event, or just a very large value in a skewed group.

Here are the amounts of ten orders from Bazaar, a fictional online store, from smallest to largest:

Output12.90, 14.95, 15.95, 34.95, 44.95, 64.95, 74.95, 164.95, 249.90, 719.92

The $719.92 order stands out when you look. The IQR rule confirms it with numbers.

How does the IQR method find outliers?

Follow four steps:

1. Find the quartiles. Q1 is the value below which 25% of the data falls. Q3 is the value below which 75% falls.

2. Calculate the IQR. IQR = Q3 - Q1. It shows how wide the middle half of the data is.

3. Set the fences. Lower fence = Q1 - 1.5 × IQR. Upper fence = Q3 + 1.5 × IQR.

4. Compare. Any value outside the fences is an outlier.

import pandas as pd

amount = pd.Series([12.9, 14.95, 15.95, 34.95, 44.95, 64.95, 74.95, 164.95, 249.9, 719.92])

q1, q3 = amount.quantile(0.25), amount.quantile(0.75)
iqr = q3 - q1
lower, upper = q1 - 1.5 * iqr, q3 + 1.5 * iqr

print(q1, q3, round(iqr, 2))
print(round(lower, 2), round(upper, 2))
print(amount[(amount < lower) | (amount > upper)].tolist())
Output20.7 142.45 121.75
-161.92 325.07
[719.92]

Q1 is 20.7, Q3 is 142.45, and the IQR is 121.75. The fences are -161.92 and 325.07. Only $719.92 is above the upper fence. The $249.90 order is large but inside the fences. A negative lower fence is normal for money data: no order can be that low, so nothing falls below it.

There are several ways to calculate quartiles. By default, pandas interpolates between the two neighboring values. If you get a slightly different result by hand, this is the reason.

How do you read a box plot?

import matplotlib.pyplot as plt

amount.plot(kind="box")
plt.show()
  • Box edges: Q1 and Q3. The height of the box is the IQR.
  • Line inside the box: the median, here 54.95.
  • Whiskers: they reach the smallest and largest values that are inside the fences, here 12.9 and 249.9. They do not extend to the fences themselves.
  • Single dots: values outside the fences. Here the $719.92 order appears as one dot.

How do you remove outliers in pandas?

Keep only the rows inside the fences:

clean = amount[(amount >= lower) & (amount <= upper)]
print(len(clean), round(clean.mean(), 2))
print(round(amount.mean(), 2), amount.median())

Nine orders remain, and their mean is $75.38. With the outlier, the mean was $139.84 and the median was $54.95.

Do not remove outliers automatically. Ask why the value is there first:

CauseExampleWhat to do
Data entry errorA decimal point in the wrong placeFix it or drop it
Test or system errorA test orderDrop it and write a note
Real but rare eventA very large business orderKeep it, and maybe analyze it separately

If the $719.92 order is real, deleting it makes your revenue look smaller than it is. You can use the median to describe a typical order, run the analysis twice (with and without the outlier) and compare, or study small and large orders as separate groups. The post on [mean, median, and mode](/blog/mean-median-mode) explains why the median resists outliers.

Should you use IQR or the z-score?

Another rule marks values more than 3 standard deviations from the mean. Try it on the same data:

z = (amount - amount.mean()) / amount.std()
print(round(amount.std(), 2), round(z.max(), 2))
print(round(amount.drop(9).std(), 2))

The z-score of the $719.92 order is 2.67, below 3, so this rule does not flag it. The outlier pulls the mean up and inflates the standard deviation to 217.59. Without it, the standard deviation is 80.81. A big outlier can hide itself by stretching the ruler that measures it, especially in small samples. The IQR is based on quartiles, which extreme values barely move, so it is usually more reliable for skewed data.

What mistakes do people make with outliers?

  • Deleting without thinking. You can throw away real and important information.
  • Treating 1.5 as a law. It is a habit. Some people use 3 × IQR to flag only extreme values.
  • Treating "outlier" as "error". The rule only says "look at this". Even in perfectly normal data, about 0.7% of values fall outside the fences.
  • Deciding with a tiny sample. With ten values, the quartiles move a lot.

What to read next

  • [Outliers](/learn/statistics-data-analysis/outliers): find the outliers in the full Bazaar orders and decide what to do with each.
  • [Quartiles, IQR, and the box plot](/learn/statistics-data-analysis/quartiles-iqr-boxplot): read the box plot step by step.
  • [Mean, median, and mode](/blog/mean-median-mode): which average to trust.
  • [Statistics and Data Analysis course](/courses/statistics-data-analysis): 44 free lessons.
  • #statistics
  • #outliers
  • #iqr
  • #box plot
All posts