Data AnalyticsData Cleaningbeginner
Updated:

Outlier Detection and Treatment

5 min read

Outliers can be typos or real extremes. Learn to detect them with the IQR and z-score methods in pandas, and decide whether to keep, cap or remove each one.

TL;DR – Quick Answer

Outlier detection is finding values that sit far from the rest of a column, and treatment is deciding what to do with them. Two standard detection methods are the IQR rule, which flags values beyond 1.5 times the interquartile range, and the z-score, which flags values more than about three standard deviations from the mean. Treatment options are to keep, cap (winsorize), or remove them, depending on whether the outlier is a genuine extreme or an error.

On This Page

An outlier is a value that sits far from the rest of a column — a spend of 5,000,000 in a table where most rows are in the hundreds, or an age of 214. Outlier detection is finding those points; treatment is deciding what to do about them. The decision is not automatic, because an outlier can be a typo to fix, a genuine extreme to keep, or the exact signal you are looking for. Handling them well protects your averages and models without erasing real information.

This topic connects to normalization vs standardization, since outliers strongly affect how those scaling methods behave, and it builds on the cleaning overview in what is data cleaning.

Two standard detection methods

The IQR method looks at the middle 50% of the data. It computes the first quartile Q1 (25th percentile) and third quartile Q3 (75th percentile), takes the interquartile range IQR = Q3 - Q1, and flags anything below Q1 - 1.5 * IQR or above Q3 + 1.5 * IQR. Because quartiles are not themselves dragged by extremes, this method is robust and works even on skewed data.

The z-score method measures how many standard deviations a value sits from the mean. Values with an absolute z-score above roughly 3 are flagged. It assumes a roughly normal distribution and, importantly, the mean and standard deviation it depends on are themselves distorted by outliers — so it is best on symmetric data.

Worked example in pandas

Here is an illustrative sample with two obvious extremes.

import pandas as pd

df = pd.DataFrame({
    "customer": ["A", "B", "C", "D", "E", "F", "G", "H"],
    "spend":    [220, 240, 250, 260, 270, 280, 300, 9800],  # 9800 is extreme
})

Q1 = df["spend"].quantile(0.25)
Q3 = df["spend"].quantile(0.75)
IQR = Q3 - Q1
low, high = Q1 - 1.5 * IQR, Q3 + 1.5 * IQR

df["is_outlier"] = (df["spend"] < low) | (df["spend"] > high)
print(f"bounds: {low:.1f} to {high:.1f}")
print(df)

Expected output:

bounds: 175.0 to 355.0
  customer  spend  is_outlier
0        A    220       False
1        B    240       False
2        C    250       False
3        D    260       False
4        E    270       False
5        F    280       False
6        G    300       False
7        H   9800        True

The IQR rule cleanly isolated the 9,800 value as an outlier while leaving the tight cluster untouched. Now you can treat it. Two common treatments:

# Option 1: cap (winsorize) the outlier at the upper bound
capped = df.copy()
capped["spend"] = capped["spend"].clip(lower=low, upper=high)

# Option 2: remove the outlier rows entirely
removed = df[~df["is_outlier"]]

print("capped max:", capped["spend"].max(), "| removed rows:", len(removed))
capped max: 355.0 | removed rows: 7

Capping with clip() kept all eight rows but pulled the extreme down to the boundary, so it no longer distorts the average. Removing dropped the row entirely. Which is right depends on whether that 9,800 is a data-entry error (fix or remove) or a real whale customer (keep, or cap only for a specific model).

How analysts decide treatment

The first question is always why the value is extreme. A negative age or an amount with an extra zero is an error — correct it or drop it. A genuinely huge order from a real customer is signal — keeping it may matter more than any tidiness. When an extreme is real but distorts a model or average, capping is often the best compromise: you keep the row and its other data while limiting the damage. Removal is a last resort reserved for clear errors, because deleting real extremes can hide exactly the fraud, churn risk or top account you were hired to find.

Analysts also report outliers rather than silently deleting them. "We capped the top 1% of spend for this model" is a defensible, documented choice; a quietly deleted row is not.

Context also decides the method. On a tight, roughly symmetric column the z-score is fine; on a heavily skewed one like income the IQR is safer, and sometimes the honest answer is to leave the outlier and report a robust statistic instead. Reporting the median alongside the mean, for example, lets a reader see the effect of extremes without any data being altered at all — often the cleanest response of all.

A quick look with describe

Before formal detection, describe() gives a fast read on whether a column even has an outlier problem. A maximum far above the 75th percentile, or a mean noticeably higher than the median, is the tell.

print(df["spend"].describe()[["mean", "50%", "75%", "max"]].round(1))
mean    1452.5
50%      265.0
75%      285.0
max     9800.0

The mean (1452.5) sitting far above the median (265) and the max towering over the 75th percentile (285) both scream that a single extreme is distorting the column — exactly what the IQR test then confirmed. Building this instinct, reading a summary and predicting where the outliers are, is faster than running a formal test on every column.

Common mistakes

  • Deleting all outliers reflexively. Real extremes are often the most important rows. Removing them can erase the signal.
  • Using z-scores on skewed data. The mean and standard deviation are themselves pulled by the outliers, so the method misses them or mislabels normal points. Prefer IQR for skewed columns.
  • Treating outliers before understanding them. Always ask whether the value is an error or a genuine extreme first; the answer changes the treatment.
  • Forgetting outliers affect scaling. Standardization uses the mean and standard deviation, both outlier-sensitive, so untreated outliers quietly distort scaled features (see normalization vs standardization).

In interviews

Outlier questions are common: "How do you detect outliers?" and "What would you do with them?" Name both the IQR and z-score methods and, crucially, explain when each applies — IQR for skewed or unknown distributions, z-score for roughly normal ones. The stronger signal is your treatment reasoning: distinguish errors from genuine extremes, mention capping as an alternative to deletion, and note that you document the choice. If handed data, showing the IQR calculation above is a clean, confident answer.

Where this fits in your learning path

Outlier handling is a mid-level skill in the data cleaning cluster that rewards judgement over syntax. It builds on what is data cleaning and directly informs normalization vs standardization, since scaling and outliers interact. Comfort with detecting and treating extremes is a valued part of the data analyst roadmap.

Frequently Asked Questions

What is the IQR method for detecting outliers?
The IQR method uses the middle 50% of the data. It computes Q1 (25th percentile) and Q3 (75th percentile), takes IQR = Q3 - Q1, and flags any value below Q1 - 1.5*IQR or above Q3 + 1.5*IQR as an outlier. It is robust because quartiles are not distorted by extreme values themselves.
When should I use z-score instead of IQR?
The z-score works best when the column is roughly normally distributed, flagging values more than about three standard deviations from the mean. The IQR method is safer for skewed data because the mean and standard deviation it relies on are themselves pulled by outliers, whereas quartiles are not.
Should I always remove outliers?
No. Some outliers are genuine and important, such as a real high-value customer or a fraud case you are trying to detect. Removing them can hide the very signal you need. Only remove outliers that are clearly errors, and consider capping rather than deleting when the extreme is real but distorting.
What is winsorizing or capping?
Capping, or winsorizing, replaces extreme values with a boundary value instead of deleting the row, for example setting everything above the 99th percentile to the 99th percentile value. It keeps the row and its other columns while limiting the outlier's distorting effect on averages and models.
How do outliers affect the mean and standard deviation?
Both are highly sensitive to outliers. A single very large value pulls the mean upward and inflates the standard deviation, which can make a typical value look unusual and vice versa. This is why the median and IQR, which resist extremes, are preferred for describing and detecting outliers in skewed data.

Want to Build Your Career in Data Analytics with AI?

Join CodeBegun and train with working industry engineers — Check the Data Analyst training details

Apply for Demo Class →
Siva Prasad Galaba
Founder, CodeBegun · Staff Engineer

Founder of CodeBegun. 15+ years building Java systems at companies like Crunchyroll. Teaches Java, Spring Boot and system design the way the industry actually works, and mentors students through projects, mock interviews and placement preparation.

Technically reviewed by CodeBegun Technical TeamLast reviewed 16 July 2026 LinkedIn
Chat with us