Split US phone numbers into area, prefix and line columns with pandas str.extract

Quick Overview

Write a pandas function that splits US phone strings into area code, prefix and line number columns using one regex with three capture groups. It tests str.extract output shapes, escaping literal characters, column naming and how rows that do not match are handled.

Split US phone numbers into area, prefix and line columns with pandas str.extract

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `def split_phone_parts(s: pd.Series) -> pd.DataFrame` that parses US phone numbers into three columns. A valid number looks like `(415) 555-1234`: a 3-digit area code in parentheses, optional whitespace, a 3-digit prefix, a hyphen, and a 4-digit line number. The function must: - Use `s.str.extract` with one capture group per part, so each group becomes one column. - Return a `DataFrame` with columns `['area', 'prefix', 'line']` in that order. - Produce `NaN` in all three columns for rows that do not match. - Preserve the index of the input Series. For example: ```python s = pd.Series(['(415) 555-1234', '(212)999-0000', 'bad']) split_phone_parts(s) # area prefix line # 0 415 555 1234 # 1 212 999 0000 # 2 NaN NaN NaN ``` ```hint Groups versus formatting Go through `(415) 555-1234` character by character and decide what belongs inside a capture group and what is formatting the pattern must match literally. The second example row shows that the space after the area code is optional. ``` ### What a Strong Answer Covers - A pattern with one capture group per part, escaped literal parentheses, and the formatting characters kept outside the groups. - Handling the optional whitespace after the area code, as in `(212)999-0000`. - Naming the columns `area`, `prefix` and `line` in order, either by renaming or with named groups. - What happens to rows that do not match, and how the input index carries through. - Why the parts stay strings, for example the line number `0000`. ### Follow-up Questions - How would you also capture an optional country code prefix? - What does `str.extractall` do differently here? - The pattern matches anywhere inside a string. How would you reject values such as `(415) 555-12345` or a number buried in other text?

Overview: Write a pandas function that splits US phone strings into area code, prefix and line number columns using one regex with three capture groups. It tests str.extract output shapes, escaping literal characters, column naming and how rows that do not match are handled.

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

Implement def split_phone_parts(s: pd.Series) -> pd.DataFrame that parses US phone numbers into three columns. A valid number looks like (415) 555-1234: a 3-digit area code in parentheses, optional whitespace, a 3-digit prefix, a hyphen, and a 4-digit line number.

The function must:

  • Use s.str.extract with one capture group per part, so each group becomes one column.
  • Return a DataFrame with columns ['area', 'prefix', 'line'] in that order.
  • Produce NaN in all three columns for rows that do not match.
  • Preserve the index of the input Series.

For example:

s = pd.Series(['(415) 555-1234', '(212)999-0000', 'bad'])
split_phone_parts(s)
#    area prefix  line
# 0   415    555  1234
# 1   212    999  0000
# 2   NaN    NaN   NaN

What a Strong Answer Covers Guidance

  • A pattern with one capture group per part, escaped literal parentheses, and the formatting characters kept outside the groups.
  • Handling the optional whitespace after the area code, as in (212)999-0000 .
  • Naming the columns area , prefix and line in order, either by renaming or with named groups.
  • What happens to rows that do not match, and how the input index carries through.
  • Why the parts stay strings, for example the line number 0000 .

Follow-up Questions Guidance

  • How would you also capture an optional country code prefix?
  • What does str.extractall do differently here?
  • The pattern matches anywhere inside a string. How would you reject values such as (415) 555-12345 or a number buried in other text?