Data Analyst Interview
Menu
Browse in your language. All mock interviews, preparation sessions and feedback are in English only.

Audit Duplicate Order Keys Before Using pandas drop_duplicates

Python • Data quality

Audit Duplicate Order Keys Before Using pandas drop_duplicates

Separate repeated payloads from conflicting amounts before choosing which records belong in a report.

By Prakhar Shrivastava · October 16, 2026

Use duplicated(keys, keep=False) to inspect every row sharing a business key before deleting anything. A repeated key may identify a replayed record, a correction, or legitimate order lines. The default drop_duplicates keeps the first occurrence; that row is not necessarily the correct or newest record.

What counts as a duplicate?

This fictional reporting extract contains US, UK, and German orders. All amounts are already in USD. Its contract says there should be one record per market and order ID, and the three displayed columns are the complete business payload. Order IDs are not assumed unique across markets.

import pandas as pd
x = pd.DataFrame({
    'market': ['US', 'US', 'UK', 'UK', 'DE'],
    'order_id': [101, 101, 202, 202, 303],
    'amount_usd': [75, 75, 50, 60, 40]
})
keys = ['market', 'order_id']
audit = x[x.duplicated(keys, keep=False)]
print(audit)
# US 101 appears twice at $75
# UK 202 appears at $50 and $60

The audit contains four rows. The US records have identical payloads. The UK records disagree about the amount. A total of all five rows would count both copies and both conflicting versions, so it is not a validated order-revenue total.

Remove identical payload repeats only under an explicit rule

For this exercise, the source owner has confirmed that identical full payloads are ingestion replays and should count once. That rule allows us to remove an exact repeat before checking the remaining business keys.

exact = x.drop_duplicates()
conflict = exact[exact.duplicated(keys, keep=False)]
accepted = exact[~exact.duplicated(keys, keep=False)]
print(conflict['amount_usd'].tolist())
# [50, 60]
print(accepted['amount_usd'].tolist())
# [75, 40]

Four rows remain after removing the identical replay. Both UK versions go into the conflict table. The accepted subset contains the US $75 order and the German $40 order, totaling $115. This is a subtotal for accepted records, not the total for all orders: the UK order remains unresolved.

Keep the rejected records and a reason code so the source owner can resolve the discrepancy. Do not infer that $60 is correct because it is larger or appears later. If revisions have an authoritative version number or timestamp, define and test a precedence rule, including ties, before selecting a winner.

Why keep=False has two very different uses

print(x.drop_duplicates(keys, keep=False)['amount_usd'].tolist())
# [40]
print(x.drop_duplicates(keys)['amount_usd'].tolist())
# [75, 50, 40]

With duplicated, keep=False marks every occurrence of a repeated key. With drop_duplicates, keep=False removes every occurrence of a repeated key. Applied directly to the original data, that deletion removes both the valid $75 replay group and the unresolved UK group, leaving only Germany.

The default keep=’first’ returns three rows and silently chooses $50 for the UK order. Choosing keep=’last’ would select $60 instead. Neither setting validates the source. “First” and “last” refer to the current row order; sorting must reflect a justified business rule if order is meant to determine precedence.

Adapt the audit to the real data contract

If an order can contain multiple lines, market and order ID are not a unique row key. Include line ID when auditing line records, or deliberately aggregate lines to order level. Deleting repeated order IDs from a line table can discard legitimate sales; see the order-grain AOV example.

Exports may contain ingestion timestamps or file names that differ between otherwise identical business payloads. Specify the payload columns deliberately instead of assuming every column defines identity. Preserve provenance separately. pandas ignores the index when dropping duplicates, so an identifier stored only in the index must be made part of your explicit audit design. Missing key values need their own exception check before a record is accepted.

Verify the result before joining or reporting

Six fixture checks passed in pandas 2.2.3: four flagged input rows, four rows after exact deduplication, UK conflict amounts [50, 60], accepted amounts [75, 40], direct keep=False leaving [40], and default key deduplication choosing [75, 50, 40]. These assertions verify the example’s behavior; they do not prove an unknown production record is a replay.

After resolving exceptions, validate the expected key uniqueness before downstream joins. The pandas merge validation guide covers join multiplication, while Series assignment and index alignment explains why equal totals can still hide incorrect attribution.

Primary references: DataFrame.duplicated and DataFrame.drop_duplicates. Audit identity and conflicting values first; make deletion a documented consequence of that audit.

Leave a Comment

✉ WhatsApp