Reference · Python (pandas)
Remove duplicates with a survivor rule in Python (pandas)
Keep one row per key, chosen by a complete deterministic order, and report the others.
When to use it
Repeated records at the table's grain: re-sent rows, versions, the same customer from several systems.
How
Sort by the survivor rule with a stable sort, then Series.duplicated on the key.
df = df.sort_values(["version"], ascending=False, kind="mergesort") loser = df.duplicated(["order_id", "line_id"])
- duplicated keeps the first occurrence in the current order.
Pitfalls
- Deduplicating at the wrong grain.
- An incomplete order that leaves ties to chance.
- Comparing numbers or timestamps as text.
Lesson: Find the row grain before removing duplicates
Practise it: Deduplicate order lines at the right grain