Split a full-name Series into first and last columns with pandas

Quick Overview

Write a pandas function that splits a Series of full names on the first space into first and last columns while keeping multi-word surnames and the original index. It tests vectorized string methods, output shape when no value contains a space, and handling of missing values and stray whitespace.

Split a full-name Series into first and last columns with pandas

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `split_full_name(s: pd.Series) -> pd.DataFrame` that splits each `'First Last'` string on the first space into two columns. Any remaining spaces stay in the second part. Requirements: - Use `s.str.split(' ', n=1, expand=True)`. - Name the output columns `['first', 'last']`. - A value with no space yields the whole string in `first` and `None` in `last`. - Preserve the original index. Example: ```python s = pd.Series(['Alice Smith', 'Bob Van Der Berg', 'Carol']) split_full_name(s) # first last # 0 Alice Smith # 1 Bob Van Der Berg # 2 Carol None ``` ```hint Check the shape of the split With `expand=True`, pandas creates only as many columns as the longest split needs. Think about a Series in which no name contains a space. ``` ### What a Strong Answer Covers - Limits the split to the first space so multi-word surnames stay whole in `last`. - Returns exactly the columns `first` and `last`, aligned to the original index. - Handles single-token values, including a Series where no value has a space and an empty Series. - Explains what happens to missing values and to stray whitespace. ### Follow-up Questions - How would you handle leading or trailing whitespace in the input? - What if some entries are `NaN`? - Which real names does a first-space rule get wrong, and what would you do about them?

Overview: Write a pandas function that splits a Series of full names on the first space into first and last columns while keeping multi-word surnames and the original index. It tests vectorized string methods, output shape when no value contains a space, and handling of missing values and stray whitespace.

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

Implement split_full_name(s: pd.Series) -> pd.DataFrame that splits each 'First Last' string on the first space into two columns. Any remaining spaces stay in the second part.

Requirements:

  • Use s.str.split(' ', n=1, expand=True) .
  • Name the output columns ['first', 'last'] .
  • A value with no space yields the whole string in first and None in last .
  • Preserve the original index.

Example:

s = pd.Series(['Alice Smith', 'Bob Van Der Berg', 'Carol'])
split_full_name(s)
#     first          last
# 0   Alice         Smith
# 1     Bob  Van Der Berg
# 2   Carol          None

What a Strong Answer Covers Guidance

  • Limits the split to the first space so multi-word surnames stay whole in last .
  • Returns exactly the columns first and last , aligned to the original index.
  • Handles single-token values, including a Series where no value has a space and an empty Series.
  • Explains what happens to missing values and to stray whitespace.

Follow-up Questions Guidance

  • How would you handle leading or trailing whitespace in the input?
  • What if some entries are NaN ?
  • Which real names does a first-space rule get wrong, and what would you do about them?