Deduplicate Rows by Case-Insensitive Email in pandas, Keeping Original Values

Quick Overview

Write a pandas function that removes duplicate rows by email when addresses differ only in case or surrounding spaces, keeping the first row and its original values. It tests building a separate matching key, how drop_duplicates picks survivors, and resetting the index.

Deduplicate Rows by Case-Insensitive Email in pandas, Keeping Original Values

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `def dedup_case_insensitive_email(df: pd.DataFrame) -> pd.DataFrame`, where `df` has columns `['email', 'plan']`. Deduplicate the rows on a normalized email key, so that addresses that differ only in surrounding whitespace or letter case count as the same address. The function must meet these requirements: - The normalized key is `email.str.strip().str.lower()`. - Keep the first row for each key. - Output the original `email` and `plan` values. The normalized email is only used for matching. - The result has exactly the columns `['email', 'plan']`, with no helper column. - Reset the index to `0..n-1`. For example: ```python df = pd.DataFrame({ 'email': [' Bob@X.com ', 'bob@x.com', 'ann@y.com'], 'plan': ['pro', 'free', 'free'], }) out = dedup_case_insensitive_email(df) out.to_dict('records') # [{'email': ' Bob@X.com ', 'plan': 'pro'}, {'email': 'ann@y.com', 'plan': 'free'}] out.index.tolist() # [0, 1] ``` The first two rows share the key `bob@x.com`, so only the first one survives, with its original spacing and case. ```hint Separate matching from output Compute the key as its own Series and use it only to decide which rows to keep. Then check what the index looks like after rows have been dropped. ``` ### Constraints & Assumptions - `df` has exactly the columns `email` and `plan`, and `email` holds strings. - "First" means the earliest row in the input order. ### What a Strong Answer Covers - Building the key with `str.strip().str.lower()` without overwriting `email`. - Keeping the first row per key in input order. - Returning only the original columns, with their original values. - Resetting the index, and how missing emails behave. ### Follow-up Questions - How would you keep the last occurrence instead of the first? - How would you extend normalization to Gmail-style dot and plus aliases? - Two rows for the same address have different plans. How would you keep the row with the better plan, whatever its position?

Overview: Write a pandas function that removes duplicate rows by email when addresses differ only in case or surrounding spaces, keeping the first row and its original values. It tests building a separate matching key, how drop_duplicates picks survivors, and resetting the index.

Nov 24, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Implement def dedup_case_insensitive_email(df: pd.DataFrame) -> pd.DataFrame, where df has columns ['email', 'plan']. Deduplicate the rows on a normalized email key, so that addresses that differ only in surrounding whitespace or letter case count as the same address.

The function must meet these requirements:

  • The normalized key is email.str.strip().str.lower() .
  • Keep the first row for each key.
  • Output the original email and plan values. The normalized email is only used for matching.
  • The result has exactly the columns ['email', 'plan'] , with no helper column.
  • Reset the index to 0..n-1 .

For example:

df = pd.DataFrame({
    'email': ['  Bob@X.com ', 'bob@x.com', 'ann@y.com'],
    'plan':  ['pro',          'free',      'free'],
})

out = dedup_case_insensitive_email(df)
out.to_dict('records')
# [{'email': '  Bob@X.com ', 'plan': 'pro'}, {'email': 'ann@y.com', 'plan': 'free'}]
out.index.tolist()
# [0, 1]

The first two rows share the key bob@x.com, so only the first one survives, with its original spacing and case.

Constraints & Assumptions

  • df has exactly the columns email and plan , and email holds strings.
  • "First" means the earliest row in the input order.

What a Strong Answer Covers Guidance

  • Building the key with str.strip().str.lower() without overwriting email .
  • Keeping the first row per key in input order.
  • Returning only the original columns, with their original values.
  • Resetting the index, and how missing emails behave.

Follow-up Questions Guidance

  • How would you keep the last occurrence instead of the first?
  • How would you extend normalization to Gmail-style dot and plus aliases?
  • Two rows for the same address have different plans. How would you keep the row with the better plan, whatever its position?