← All Python interview questions
De-duplicate a signup table in pandas
MediumPandas — CleaningZohoFreshworks
A signup export has duplicate emails (the same person submitted the form twice) and a few rows with a missing name.
Write a function clean_signups(df) that returns a cleaned DataFrame: drop rows where name is null, then keep only the first row for each email. Reset the index so it runs 0..n-1.
This fixture runs before your code on every Run and Submit, so the data is always clean.
import pandas as pd
df = pd.DataFrame(
{
"email": [
"ira@x.com",
"dev@x.com",
"ira@x.com",
"meh@x.com",
"sag@x.com",
"dev@x.com",
],
"name": ["Ira", "Devansh", "Ira B", None, "Sagar", "Devansh R"],
"plan": ["pro", "free", "pro", "free", "pro", "free"],
}
)- Step 1: `dropna(subset=["name"])` removes rows missing a name without touching nulls in other columns — always scope `dropna` with `subset`.
- Step 2: Order matters. Dropping null names *before* de-duplicating means a null-name row can never win the `keep="first"` race for its email.
- Step 3: `drop_duplicates(subset=["email"], keep="first")` keeps the earliest signup per email.
- Step 4: `reset_index(drop=True)` renumbers the surviving rows; `drop=True` stops the old index becoming a column.
- Step 5: Every step returns a new DataFrame, so the caller's original data is untouched.
import pandas as pd
def clean_signups(df):
cleaned = df.dropna(subset=["name"])
cleaned = cleaned.drop_duplicates(subset=["email"], keep="first")
return cleaned.reset_index(drop=True)Learn the concept first
Stuck on Pandas — Cleaning? These free Python lessons teach it step by step.
More Pandas — Cleaning questions
Asked at Zoho
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