Strip whitespace and title-case a pandas Series of full names
Quick Overview
Write a pandas function that trims surrounding whitespace from a column of full names and converts each value to title case, keeping missing values, the index and the name. It tests chained string methods, missing-value handling and the name formats that plain title-casing gets wrong.
Strip whitespace and title-case a pandas Series of full names
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Implement `titlecase_names(s: pd.Series) -> pd.Series` that normalises a column of full names: strip leading/trailing whitespace, then title-case each value using `s.str.strip().str.title()`.
Requirements:
- `NaN` values stay `NaN`.
- Preserve the Series `index` and `name`.
Example:
```python
titlecase_names(pd.Series([' alice smith ', 'BOB JONES', 'carol']))
# -> pd.Series(['Alice Smith', 'Bob Jones', 'Carol'])
```
```hint Check the word rule
Look up how `str.title()` decides where a word starts and what it does to the letters after the first one. Then try a name with a hyphen, an apostrophe or a prefix such as Mc.
```
### What a Strong Answer Covers
- Stripping surrounding whitespace and what `str.strip()` counts as whitespace.
- How `str.title()` splits words and lowercases the remaining letters, so all-caps input is fixed.
- How the `.str` accessor handles missing values and keeps the index and name.
- The names that plain title-casing gets wrong and a sensible way to limit the damage.
### Follow-up Questions
- Which real names does `str.title()` get wrong, and how would you handle them?
- How would you also collapse repeated spaces, tabs or non-breaking spaces inside a name?
- What happens to a number or other non-string value in the column, and how would you detect it?
Overview: Write a pandas function that trims surrounding whitespace from a column of full names and converts each value to title case, keeping missing values, the index and the name. It tests chained string methods, missing-value handling and the name formats that plain title-casing gets wrong.
Implement titlecase_names(s: pd.Series) -> pd.Series that normalises a column of full names: strip leading/trailing whitespace, then title-case each value using s.str.strip().str.title().
Requirements:
NaN
values stay
NaN
.
Preserve the Series
index
and
name
.
Example:
titlecase_names(pd.Series([' alice smith ', 'BOB JONES', 'carol']))
# -> pd.Series(['Alice Smith', 'Bob Jones', 'Carol'])
What a Strong Answer Covers Guidance
Stripping surrounding whitespace and what
str.strip()
counts as whitespace.
How
str.title()
splits words and lowercases the remaining letters, so all-caps input is fixed.
How the
.str
accessor handles missing values and keeps the index and name.
The names that plain title-casing gets wrong and a sensible way to limit the damage.
Follow-up Questions Guidance
Which real names does
str.title()
get wrong, and how would you handle them?
How would you also collapse repeated spaces, tabs or non-breaking spaces inside a name?
What happens to a number or other non-string value in the column, and how would you detect it?