

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 9
click for more info
Not enough gems
Cost: 6 gems
1: Data Cleaning
incomplete
2: Handling Missing Values
incomplete
3: Handling Duplicates
incomplete
4: Type Normalization
incomplete
5: Converting Types
incomplete
6: Cleaning Dates
incomplete
7: Working With Date Values
incomplete
8: Data Validation
incomplete
9: String Length
incomplete
10: Validation Summary
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
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.
"At-least-once delivery" is a common cause, but other sources include:
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")
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"])
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.