Outlier Detection in Python
Find and handle extreme values that distort your analysis using IQR and Z-score
Outliers are data points that are far from the rest of the data. They can be genuine extreme values (a CEO salary in an employee dataset) or data errors (a typo: 750000 instead of 75000). Not handling outliers leads to skewed means, misleading charts, and inaccurate models.
Two common methods: IQR (Interquartile Range) — robust, non-parametric; Z-score — assumes approximately normal distribution.
Example
Key Points
- ✓IQR method: outliers are < Q1 - 1.5×IQR or > Q3 + 1.5×IQR — same as box plot whiskers
- ✓Z-score: values with |z| > 3 are usually outliers (beyond 3 standard deviations)
- ✓Always investigate before removing — some outliers are real data, not errors
- ✓Options: remove, cap (winsorize), or log-transform the column
- ✓Box plot visually shows outliers as individual dots beyond the whiskers
Practice Question
A salary column has Q1=40000, Q3=80000. What is the IQR upper fence for outlier detection?