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.
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 IQR rule (Tukey, 1977). The quartiles and mark the middle half of the values. Their distance apart is the interquartile range:
and a value is flagged if it's outside the fences:
The z-score rule. How many standard deviations a value is from the mean, flagged if :
The modified z-score (Iglewicz and Hoaglin, 1993) swaps the mean for the median , and the standard deviation for the median absolute deviation, the median distance from the median. It's flagged if :
The 0.6745 makes match for data shaped like a bell curve. Run all three on the store's December:
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.
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.
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
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.
- NIST/SEMATECH e-Handbook of Statistical Methods: Detection of outliers: the modified z-score and its 3.5 cut-off (Iglewicz and Hoaglin)
- Shiffler, Maximum Z Scores and Outliers, The American Statistician 42(1), 1988: why no z-score in a small sample can be large
- pandas: pandas.DataFrame.duplicated: subset and keep
- pandas: pandas.Series.quantile: how the quartiles are worked out