Outliers & Validation

Python · Data Cleaning

Theory

Validation checks that data follows the rules you expect; outlier detection finds values that are technically valid but suspiciously far from the rest. Simple rules are boolean masks: df[~df['amount'].between(0, 100000)] lists amounts outside a sensible range, df[df['email'].isna()] finds missing emails and df['order_id'].is_unique confirms a key.

The IQR rule is a standard outlier test that is not distorted by the outliers themselves. Compute q1 = s.quantile(0.25), q3 = s.quantile(0.75) and iqr = q3 - q1; values below q1 - 1.5*iqr or above q3 + 1.5*iqr are outliers. With amounts 950, 1000, 1100, 1200, 1250 and 98000, only 98000 is flagged. A z-score ((x - mean) / std) above 3 is another rule, but one huge value inflates the std and can hide itself in small samples. After finding outliers, decide: fix, cap with clip(), drop, or keep and report.

Why we use it

A single wrong value (an extra zero, a test order, a negative quantity) can wreck an average or a chart. Rule checks and outlier tests catch these early, before they reach a report or a model.

When to use it

Run rule checks after loading and after each join (keys unique, no unexpected nulls, values in range). Use the IQR rule for skewed business numbers like order amounts; never drop outliers blindly - big customers are often real.

How it works

  1. Numeric column to validate
  2. Check hard rules: ranges, not null, unique keys
  3. Compute q1, q3 and iqr = q3 - q1
  4. Outside q1 - 1.5*iqr .. q3 + 1.5*iqr? Yes: Flag as outlier, investigate. No: Keep the row.
  5. Fix, cap, drop or keep each flagged value
  6. Validated data

Syntax

s.between(low, high)
q1, q3 = s.quantile([0.25, 0.75])
iqr = q3 - q1
df[s.between(q1 - 1.5*iqr, q3 + 1.5*iqr)]
df['key'].is_unique

Example

q1, q3 = df['amount'].quantile([0.25, 0.75])
iqr = q3 - q1
clean = df[df['amount'].between(q1 - 1.5*iqr, q3 + 1.5*iqr)]

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic