BasicsPandasDeduplicationMetaAmazonUber
Problem
Implement dedup_by_email(df) that removes duplicate rows sharing the same email, keeping the first occurrence and returning a clean 0-based reset index.
Why It Matters: Deduplicating user or event logs by natural key (email, event_id) prevents double-counting revenue and leaking duplicates into train/test splits.
Function Signature
import pandas as pd
def dedup_by_email(df: pd.DataFrame) -> pd.DataFrame:
Sample Input & Output
Example 1: Duplicate Emails Removed
Input:
| user_id | |
|---|---|
| 1 | a@x.com |
| 2 | b@x.com |
| 3 | a@x.com |
Expected Output:
| user_id | |
|---|---|
| 1 | a@x.com |
| 2 | b@x.com |
Explanation: Row 3 (user_id = 3) duplicates 'a@x.com' and is dropped; Row 1 is kept.
Example 2: Already Unique Emails
Input:
| user_id | |
|---|---|
| 10 | x@y.com |
| 20 | z@y.com |
Expected Output:
| user_id | |
|---|---|
| 10 | x@y.com |
| 20 | z@y.com |
Explanation: All rows are unique so the DataFrame is returned unchanged.
Constraints & Edge Cases
- Keep the first row for each unique
email. - Use
drop_duplicates(subset=['email'], keep='first').reset_index(drop=True). - Preserve original relative order of kept rows.
Constraints & Evaluation Criteria
- Keep the first row for each unique `email`.
Hints
Python 3⌘/Ctrl+Enter to Run · ⌘/Ctrl+Shift+Enter to Submit
Loading Code Editor...
+1 Hidden Submit Case
Input / Scenario
3 rows, 1 dup email
Expected Output / Criteria
2 rows