We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Handling Duplicates

Duplicate records are sadly very common. In distributed systems, it's often easier to allow an event to be recorded more than once than to guarantee it happens exactly once... but they can obviously ruin your analysis.

Why Do Duplicates Occur?

"At-least-once delivery" is a common cause, but other sources include:

  • An Excel jockey accidentally copies and pastes the same rows twice
  • A Database export has duplicate rows due to an erroneous join
  • Webhook retries create repeated records

Detecting Duplicates

Pandas gives you the duplicated() method, which returns a boolean Series: each True value indicates a duplicate row (or duplicate key) in the DataFrame. So, you can use it to filter them out:

duplicates_mask = df.duplicated()
# [False, False, True, False, True, ...]

df = df[duplicates_mask]  # Keep only the duplicates

Alternatively, you can only check for duplicates based on specific columns:

duplicates_mask = df.duplicated(subset=["order_id", "purchase_time"])

Or, you can keep the last occurrence of each duplicate instead of the first (default):

duplicates_mask = df.duplicated(keep="last")

Removing Duplicates

The drop_duplicates() outright removes duplicate rows:

df = df.drop_duplicates()

Similarly, it has some useful options:

# keep last occurrence instead
df = df.drop_duplicates(keep="last")

# keep NO occurrences
df = df.drop_duplicates(keep=False)

# only consider specific columns for duplicates
df = df.drop_duplicates(subset=["order_id", "purchase_time"])

Assignment

SnackStack has duplicate device records, making the team think more devices "passed" QA than actually did.

Complete the get_batch_pass_rate function. It accepts a device DataFrame (with device_id and status columns), removes duplicate device records, and returns the accurate batch "pass rate" as a percentage.