Split a Full Name Column Into First and Last Name Columns in Pandas
Quick Overview
Implement a pandas function that splits a full_name column into separate first and last name columns. It tests vectorized string splitting with expand, exact column names and object dtypes for a strict frame comparison, keeping the original index, and leaving the input unchanged.
Split a Full Name Column Into First and Last Name Columns in Pandas
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Implement `split_full_name(df: pd.DataFrame) -> pd.DataFrame`. The input has a single column `'full_name'` where each value has exactly one space separating first and last name. Return a new DataFrame split via `df['full_name'].str.split(' ', n=1, expand=True)` and renamed.
The result must meet these requirements:
- Columns named exactly `'first'` and `'last'`, both dtype `object`.
- Preserve the original index of `df`.
- Do not mutate the input.
The output is graded with `assert_frame_equal`.
Example:
```python
df = pd.DataFrame({'full_name': ['Alice Smith', 'Bob Jones']})
# Output:
# first last
# 0 Alice Smith
# 1 Bob Jones
```
```hint What the grader compares
Check the column labels and the dtypes that the split actually returns, and compare them with everything `assert_frame_equal` checks, which includes more than the values.
```
### What a Strong Answer Covers
- Splits with the vectorized `.str.split` using `n=1` and `expand=True`, with no Python loop over rows.
- Renames the two resulting columns to exactly `first` and `last`.
- Makes sure both columns have `object` dtype and the index equals `df.index`, since the grader compares names, dtypes and index as well as values.
- Leaves `df` unchanged and returns a separate DataFrame.
### Follow-up Questions
- How would you handle a name with a middle name, such as "Mary Ann Lee"? Which column should get the extra word?
- What happens to a single-word name or a missing value in `full_name`?
- What does your function return for an empty DataFrame?
- How would you split on the last space instead of the first one?
Overview: Implement a pandas function that splits a full_name column into separate first and last name columns. It tests vectorized string splitting with expand, exact column names and object dtypes for a strict frame comparison, keeping the original index, and leaving the input unchanged.
Implement split_full_name(df: pd.DataFrame) -> pd.DataFrame. The input has a single column 'full_name' where each value has exactly one space separating first and last name. Return a new DataFrame split via df['full_name'].str.split(' ', n=1, expand=True) and renamed.
The result must meet these requirements:
Columns named exactly
'first'
and
'last'
, both dtype
object
.
Preserve the original index of
df
.
Do not mutate the input.
The output is graded with assert_frame_equal.
Example:
df = pd.DataFrame({'full_name': ['Alice Smith', 'Bob Jones']})
# Output:
# first last
# 0 Alice Smith
# 1 Bob Jones
What a Strong Answer Covers Guidance
Splits with the vectorized
.str.split
using
n=1
and
expand=True
, with no Python loop over rows.
Renames the two resulting columns to exactly
first
and
last
.
Makes sure both columns have
object
dtype and the index equals
df.index
, since the grader compares names, dtypes and index as well as values.
Leaves
df
unchanged and returns a separate DataFrame.
Follow-up Questions Guidance
How would you handle a name with a middle name, such as "Mary Ann Lee"? Which column should get the extra word?
What happens to a single-word name or a missing value in
full_name
?
What does your function return for an empty DataFrame?
How would you split on the last space instead of the first one?