Standardize US country spellings in a pandas Series with replace
Quick Overview
Write a pandas function that unifies US country spellings such as USA and United States to a single label while leaving every other value untouched. It tests the difference between Series.replace and Series.map, whole-value versus substring matching, and keeping the index, name and dtype of the input.
Standardize US country spellings in a pandas Series with replace
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Implement `standardize_country(s: pd.Series) -> pd.Series`. Given a Series of country strings, return a new Series where the US variants are unified to `'US'` and all other values are unchanged. Use `s.replace(...)` with a mapping dict, not `s.map(...)` (which would turn unmapped values into `NaN`).
Requirements:
- Map `'USA'`, `'U.S.A.'`, and `'United States'` to `'US'`.
- Leave all other values (for example `'UK'`, `'Canada'`) exactly as they are.
- Do not mutate the input.
- Preserve the original index, `name`, and `object` dtype.
Example:
```python
s = pd.Series(['USA', 'U.S.A.', 'United States', 'UK', 'Canada'])
standardize_country(s)
# 0 US
# 1 US
# 2 US
# 3 UK
# 4 Canada
# dtype: object
```
```hint Whole value or substring
Decide whether each mapping should apply to the whole value or to any part of it. Consider a label such as `'United States Virgin Islands'`.
```
### What a Strong Answer Covers
- Uses `Series.replace` with a dict so only values that exactly equal a key change.
- Explains why `map` with the same dict loses the non-US labels.
- Returns a new Series with the input's index, name and dtype, without `inplace=True`.
- Names the variants an exact mapping misses (case, spacing, other spellings) and how to catch them without altering other values.
### Follow-up Questions
- What breaks if you used `map` instead of `replace` here?
- How would you make matching case-insensitive and whitespace-tolerant?
- On a large dataset, how would you find the US spellings you have not mapped yet?
Overview: Write a pandas function that unifies US country spellings such as USA and United States to a single label while leaving every other value untouched. It tests the difference between Series.replace and Series.map, whole-value versus substring matching, and keeping the index, name and dtype of the input.
Implement standardize_country(s: pd.Series) -> pd.Series. Given a Series of country strings, return a new Series where the US variants are unified to 'US' and all other values are unchanged. Use s.replace(...) with a mapping dict, not s.map(...) (which would turn unmapped values into NaN).
Requirements:
Map
'USA'
,
'U.S.A.'
, and
'United States'
to
'US'
.
Leave all other values (for example
'UK'
,
'Canada'
) exactly as they are.
Do not mutate the input.
Preserve the original index,
name
, and
object
dtype.
Example:
s = pd.Series(['USA', 'U.S.A.', 'United States', 'UK', 'Canada'])
standardize_country(s)
# 0 US
# 1 US
# 2 US
# 3 UK
# 4 Canada
# dtype: object
What a Strong Answer Covers Guidance
Uses
Series.replace
with a dict so only values that exactly equal a key change.
Explains why
map
with the same dict loses the non-US labels.
Returns a new Series with the input's index, name and dtype, without
inplace=True
.
Names the variants an exact mapping misses (case, spacing, other spellings) and how to catch them without altering other values.
Follow-up Questions Guidance
What breaks if you used
map
instead of
replace
here?
How would you make matching case-insensitive and whitespace-tolerant?
On a large dataset, how would you find the US spellings you have not mapped yet?