liveBook · Manning

Anomaly

As explained in Pandas Workout: 200 exercises to make you a stronger data analyst

An outlier is an observation unusually far below or above the central range of a dataset according to a chosen criterion.

An outlier is an observation unusually far below or above a dataset’s central range. Because no single definition fits every dataset, choose a criterion that matches the question. section 2.17

A common rule uses the interquartile range (IQR): values below Q1 − 1.5 × IQR or above Q3 + 1.5 × IQR are outliers. Mean-and-standard-deviation or z-score rules are alternatives. section 2.17 section 2.20

Work through an outlier analysis in four steps:

  • Create a DataFrame from aligned series, such as trip distances and passenger counts. section 2.18
  • Calculate Q1, Q3, and the IQR for the column being examined; do not calculate quantiles for unrelated columns. section 2.18
  • Filter rows with a Boolean condition for unusually low or high values. section 2.18
  • Use the selected rows to compare related measures, such as passenger counts for unusual trips versus all trips. section 2.17

Taxi-trip distances

For 10,000 taxi rides, the IQR rule finds no low-distance outliers but identifies 1,889 high-distance outliers—about 19% of rides. The next analysis compares their passenger count with the overall mean. section 2.18 section 2.17

Temperature boxplots

A boxplot marks Chicago minimum temperatures at least 1.5 × IQR beyond the quartiles. Readings of −27°C and −28°C on January 30 and 31 appear as unusually cold values. section 11.6

iqr = (
    df['trip_distance'].quantile(0.75) -
    df['trip_distance'].quantile(0.25)
)

Calculate the IQR directly from the selected DataFrame column before applying the outlier bounds. section 2.18

How Pandas Workout: 200 exercises to make you a stronger data analyst covers it

How deep it goes
Explains IQR thresholds, pandas filtering, comparative analysis, boxplots, and z-score extensions without developing statistical theory.
Examples
Uses taxi distances and passenger counts, plus Chicago temperature readings visualized with boxplots.
What it assumes
Assumes familiarity with pandas Series, DataFrames, indexes, quantiles, and Boolean selection; reinforces assembling aligned series into a DataFrame.
What you can do after
Diagnose unusual observations with pandas, compare related measures, visualize extremes, and experiment with percentile or z-score criteria.
How it connects
Connects outliers with IQR, quantiles, median, mean, standard deviation, z-score, Boolean filtering, and boxplots.

Also explained in

Compare how 14 books explain anomaly →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pandas Workout: 200 exercises to make you a stronger data analyst. The section links point to the parts of the book it draws on.