Getting and cleaning data · lesson 7 of 11
Parts in this lesson
Adf.duplicated(subset=[...], keep=False)every row that shares a key
Bs.quantile([0.25, 0.75])the quartiles, for the IQR rule
C(s - s.median()).abs().median()the median absolute deviation

Duplicates, errors and outliers

Last updated

Some bad values are easy: a row in twice, stock below zero. Others are only unusual, and unusual isn't wrong. In this lesson you'll find duplicates by key, catch impossible values, flag outliers with three rules, see why the z-score rule can hide the very outlier it's looking for, and decide what to do with a record-breaking day.

By the end of this lesson you can

  • Find exact duplicates, and rows that repeat a key with different values
  • Flag impossible values with rules you can explain
  • Flag outliers with the IQR rule, the z-score and the modified z-score
  • Tell an error from a real extreme, and log every decision

Duplicates and impossible values

Run the cells.

errors.ipynb
  • duplicated(keep=False) marks every copy of a row that appears more than once, so you see both: Lipa's rice on 2 December, twice, identical. Drop one.
  • duplicated(subset=["date", "store", "item"], keep=False) looks only at the key: the columns that should be unique together. It also finds 3 December, where Lipa's rice is in twice with different kilos, 51 and 15. That's not a copy; it's a conflict, and dropping either row is a guess. Ask the store, or keep the one with a reason (maybe 15 was typed for 51).
  • Impossible values break a rule that can never be broken: stock on hand can't be −4, and oil doesn't cost ₱120,000 a litre. Write each rule down as a mask, and look at what it catches. ₱120,000 is probably ₱120.00 with the point lost, but "probably" goes in the log.

Three outlier rules

An outlier is far from the rest. It might be a mistake, or the most interesting day in the data. These rules only find them; deciding is your job. Step through the first rule:

The interquartile range fence. A store’s December, 31 days. Most days sell 45 to 65 kg, rising towards Christmas.485147535046554952504754514856586062616466707515240575349515568December kilos, by day
1 of 4 A store’s December, 31 days. Most days sell 45 to 65 kg, rising towards Christmas.

The IQR rule (Tukey, 1977). The quartiles Q1 and Q3 mark the middle half of the values. Their distance apart is the interquartile range:

IQR=Q3−Q1

and a value is flagged if it's outside the fences:

x<Q1−1.5IQRorx>Q3+1.5IQR

The z-score rule. How many standard deviations a value is from the mean, flagged if |z|>3:

z=x−x¯s

The modified z-score (Iglewicz and Hoaglin, 1993) swaps the mean for the median x˜, and the standard deviation for the median absolute deviation, the median distance from the median. It's flagged if |M|>3.5:

M=0.6745(x−x˜)MADMAD=median of |x−x˜|

The 0.6745 makes M match z for data shaped like a bell curve. Run all three on the store's December:

outliers.ipynb

All three flag one day, 24 December, at 152 kg. The IQR and the modified z-score are built from medians and quartiles, which one wild value can't move. The ordinary z-score is built from the mean and standard deviation, which every value moves, including the outlier.

When the z-score hides an outlier

Ten days, nine of them about 50 kg, and one typed as 500. Run it.

hide.ipynb

The 500 is ten times every other value, and its z-score is 2.85: not flagged. The outlier pulls the mean up and swells the standard deviation so much that it hides itself. In fact, with 10 values no z-score can ever be bigger than 2.85, however wild the value (Shiffler, 1988). The modified z-score for the same value is 202.4.

So: prefer the IQR rule or the modified z-score, especially on small groups like one store's month. Use the z-score only on many values that are roughly bell-shaped.

Wrong way: deleting the record day

All three rules flagged 24 December. It's tempting to delete it: it's "an outlier", and the month's chart looks smoother without it.

But Christmas Eve is Noche Buena, when families buy for the midnight meal, and the busiest day of the year for a food store. A quick message to the store confirms 152 kg. Deleting it would cut 152 of the month's 1,798 kg, more than 8% of its sales,, and next year's December stock would be planned without the one day that matters most.

record.ipynb

The rule: an outlier is a question, not an answer. For each flag, find out whether it's an error (fix it, or drop it, and say which) or a real extreme (keep it). Write each decision in a log, with the reason, as the second cell does. Then if someone asks why December looks the way it does, the answer is already written.

Check what you learned

Two rows have the same date, store and item, but different kilos. What are they?
Q1 is 40 and Q3 is 60. Which value does the IQR rule flag?
Why can a z-score miss a huge outlier in a small group?

Sources

Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.

All sources for Simple Data Analysis