Remove Duplicate Rows Preserving First Occurrence

Basics
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_idemail
1a@x.com
2b@x.com
3a@x.com

Expected Output:

user_idemail
1a@x.com
2b@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_idemail
10x@y.com
20z@y.com

Expected Output:

user_idemail
10x@y.com
20z@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
Loading Code Editor...
+1 Hidden Submit Case
Input / Scenario
3 rows, 1 dup email
Expected Output / Criteria
2 rows