← All Python interview questions
De-duplicate CRM leads and keep the latest touch
MediumPandas — CleaningSalesforceFreshworks
The CRM exported the same lead several times because the email casing differs and every touchpoint was logged.
Write dedupe_leads(df) that lower-cases and strips email, keeps only the most recent row per email by touched_at, and returns the result sorted by email with a reset index.
This fixture runs before your code on every Run and Submit, so the data is always clean.
import pandas as pd
leads = pd.DataFrame({
"email": [" Asha@mail.com", "asha@mail.com", "ravi@mail.com", "RAVI@mail.com ", "neha@mail.com"],
"source": ["ads", "referral", "ads", "webinar", "organic"],
"touched_at": pd.to_datetime([
"2026-01-02", "2026-03-04", "2026-02-10", "2026-01-15", "2026-02-20",
]),
})- Step 1: Normalise before de-duplicating. ' Asha@mail.com' and 'asha@mail.com' are the same person, but not to `drop_duplicates`.
- Step 2: Sort by `touched_at` descending so the most recent touch is the first row within each email.
- Step 3: `drop_duplicates('email', keep='first')` then keeps exactly that row.
- Step 4: Re-sort by email and `reset_index(drop=True)` so the output is stable and the stale index does not leak downstream.
def dedupe_leads(df):
out = df.copy()
out["email"] = out["email"].str.strip().str.lower()
out = out.sort_values("touched_at", ascending=False)
out = out.drop_duplicates("email", keep="first")
return out.sort_values("email").reset_index(drop=True)Learn the concept first
Stuck on Pandas — Cleaning? These free Python lessons teach it step by step.
Practising alone only goes so far — our live cohorts pair Python and Pandas depth with dashboards, agentic analytics projects and mock interviews.
Explore the program