Extract the Email Domain From a pandas Series With str.extract

Quick Overview

Write a pandas function that pulls the domain out of each email address in a Series with a regular expression and returns NaN for rows that do not match. It tests str.extract with a single capture group, the expand argument, index and name handling, and messy inputs such as missing values or extra @ signs.

Extract the Email Domain From a pandas Series With str.extract

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `def email_domain(s: pd.Series) -> pd.Series` that returns the domain (everything after the `@`) of each email address using `s.str.extract(r'@(.+)$', expand=False)`. Requirements: - Extract the text after `@` with a single capture group. - Rows that do not match become `NaN`. - Preserve the original index and name the result `'domain'`. Example: ```python s = pd.Series(['a@gmail.com', 'bob@company.co.uk', 'invalid']) email_domain(s) # 0 gmail.com # 1 company.co.uk # 2 NaN # Name: domain, dtype: object ``` ```hint Read the pattern closely Work out which `@` the match starts at when a string has more than one, and what `.+` needs in order to match at all. ``` ### What a Strong Answer Covers - One capture group with `str.extract` and `expand=False`, so the result is a Series - Non-matching rows (no `@`, or nothing after it) returned as `NaN` without a manual loop - The result named `'domain'` on the original index, with the input left unchanged - How the pattern behaves on awkward inputs such as several `@` signs, surrounding whitespace or missing values ### Follow-up Questions - How would you also capture the local part (before `@`) in one call? - How does `str.extract` handle `NaN` inputs and dtype? - How would you count users per domain so that `Gmail.com` and `gmail.com` land in the same bucket?

Overview: Write a pandas function that pulls the domain out of each email address in a Series with a regular expression and returns NaN for rows that do not match. It tests str.extract with a single capture group, the expand argument, index and name handling, and messy inputs such as missing values or extra @ signs.

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

Implement def email_domain(s: pd.Series) -> pd.Series that returns the domain (everything after the @) of each email address using s.str.extract(r'@(.+)$', expand=False).

Requirements:

  • Extract the text after @ with a single capture group.
  • Rows that do not match become NaN .
  • Preserve the original index and name the result 'domain' .

Example:

s = pd.Series(['a@gmail.com', 'bob@company.co.uk', 'invalid'])
email_domain(s)
# 0        gmail.com
# 1    company.co.uk
# 2              NaN
# Name: domain, dtype: object

What a Strong Answer Covers Guidance

  • One capture group with str.extract and expand=False , so the result is a Series
  • Non-matching rows (no @ , or nothing after it) returned as NaN without a manual loop
  • The result named 'domain' on the original index, with the input left unchanged
  • How the pattern behaves on awkward inputs such as several @ signs, surrounding whitespace or missing values

Follow-up Questions Guidance

  • How would you also capture the local part (before @ ) in one call?
  • How does str.extract handle NaN inputs and dtype?
  • How would you count users per domain so that Gmail.com and gmail.com land in the same bucket?