Mask the Local Part of Email Addresses in a pandas Series

Quick Overview

Write a pandas function that replaces the local part of every email address in a Series with asterisks while keeping the domain unchanged. It tests anchored regular expressions, character classes, vectorized string methods, and how missing values, index and dtype carry through.

Mask the Local Part of Email Addresses in a pandas Series

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `mask_emails(s: pd.Series) -> pd.Series` that replaces the local part of each email (everything before the first `'@'`) with `'***'`. For example, `'alice@x.com'` becomes `'***@x.com'`. The function must follow these rules: - The domain, including the `'@'`, stays unchanged. - The work is done by one vectorized `str.replace` call with a regular expression, with no Python loops and no `apply`. - The result is an object-dtype Series with the input's index and name. - `NaN` values stay `NaN`. For example: ```python import numpy as np import pandas as pd s = pd.Series(['alice@x.com', 'bob.smith@mail.org', np.nan], dtype=object) mask_emails(s) # 0 ***@x.com # 1 ***@mail.org # 2 NaN # dtype: object ``` ```hint Pin down the local part Describe the local part as a run of characters that excludes one specific character, and make sure the pattern cannot also match text after the `'@'`. ``` ### Constraints & Assumptions - The input is an object-dtype Series of strings, with missing values stored as `NaN`. - The index can be of any type, and the Series may carry a name. ### What a Strong Answer Covers - A pattern that matches exactly the text before the first `'@'` and nothing in the domain - A single vectorized call, with regex matching turned on explicitly - Missing values, index, name and dtype carried through unchanged - Behavior on unusual values: no `'@'`, a leading `'@'`, more than one `'@'`, an empty string ### Follow-up Questions - Why does an anchored pattern built on `[^@]` stop at the first `'@'`? - How would you keep the first character and mask the rest, as in `'a***@x.com'`? - How would you leave values that contain no `'@'` unchanged, and when would you want that?

Overview: Write a pandas function that replaces the local part of every email address in a Series with asterisks while keeping the domain unchanged. It tests anchored regular expressions, character classes, vectorized string methods, and how missing values, index and dtype carry through.

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

Implement mask_emails(s: pd.Series) -> pd.Series that replaces the local part of each email (everything before the first '@') with '***'. For example, 'alice@x.com' becomes '***@x.com'.

The function must follow these rules:

  • The domain, including the '@' , stays unchanged.
  • The work is done by one vectorized str.replace call with a regular expression, with no Python loops and no apply .
  • The result is an object-dtype Series with the input's index and name.
  • NaN values stay NaN .

For example:

import numpy as np
import pandas as pd

s = pd.Series(['alice@x.com', 'bob.smith@mail.org', np.nan], dtype=object)
mask_emails(s)
# 0       ***@x.com
# 1    ***@mail.org
# 2             NaN
# dtype: object

Constraints & Assumptions

  • The input is an object-dtype Series of strings, with missing values stored as NaN .
  • The index can be of any type, and the Series may carry a name.

What a Strong Answer Covers Guidance

  • A pattern that matches exactly the text before the first '@' and nothing in the domain
  • A single vectorized call, with regex matching turned on explicitly
  • Missing values, index, name and dtype carried through unchanged
  • Behavior on unusual values: no '@' , a leading '@' , more than one '@' , an empty string

Follow-up Questions Guidance

  • Why does an anchored pattern built on [^@] stop at the first '@' ?
  • How would you keep the first character and mask the rest, as in 'a***@x.com' ?
  • How would you leave values that contain no '@' unchanged, and when would you want that?