Duplicates, and what counts as one
What it is
duplicated() flags repeats and drop_duplicates() removes them, with subset to define identity and keep choosing first, last or none.
Why it matters
A duplicated fact row inflates every sum and count downstream, and duplicates in a lookup table multiply rows on a join.
In the real world: A signup form submitted twice creates two accounts for one person; counting them twice inflates the “new customers” number the CEO sees.
Visual example
DuplicatesAn order logged twice is counted twice by every sum — until it is de-duplicated.
The revenue
before: 50 + 50 + 20 = 120
after: 50 + 20 = 70
Why this matters
Nothing about 120 looks wrong. Duplicates inflate every total without an error, so de-duplicate before you aggregate.
Common misconception
Wrong idea: duplicates are harmless extra rows.
Correct idea: every sum, count and mean counts them again. Decide which columns define 'the same', then drop.
df.duplicated(subset="order_id").sum() # how many, before removing any
df.sort_values("updated_at").drop_duplicates("order_id", keep="last")De-duplicating after aggregating rather than before. Once a duplicate has been summed it cannot be distinguished from a genuinely larger value.
Practise it: Keep Only Each User's Latest Row, Revenue Per Customer From a Messy CSV