Recode Color Codes in a pandas Series With a Default for Unmatched Values

Quick Overview

Write a pandas function that recodes single-letter color codes into full color names and sends every unmatched or missing value to a default label. It tests how Series.map treats values missing from a dictionary, how to fill the gaps in one step, and how to keep the index, name and dtype of the result intact.

Recode Color Codes in a pandas Series With a Default for Unmatched Values

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `recode_with_default(s)`. Given a pandas Series of single-letter color codes, return a new Series recoded with the mapping `{'r': 'red', 'g': 'green', 'b': 'blue'}`, where every value that does not match the mapping becomes `'unknown'`. ```python import pandas as pd def recode_with_default(s: pd.Series) -> pd.Series: ... ``` Requirements: - Recode `'r'` to `'red'`, `'g'` to `'green'` and `'b'` to `'blue'`. - Every other value (including `None`) becomes `'unknown'`. - Preserve the original index and name. - The result has `object` dtype and contains no `NaN`. Example: ```python s = pd.Series(['r', 'g', 'x', 'b', None]) recode_with_default(s) # 0 red # 1 green # 2 unknown # 3 blue # 4 unknown # dtype: object ``` ```hint Compare two recoding methods Check what `Series.map` and `Series.replace` each return for a value that is not a key of the dictionary. One of them gives you a single marker you can act on. ``` ### What a Strong Answer Covers - How the chosen method treats values that are missing from the mapping - How `None` and `NaN` in the input end up labeled `'unknown'` - Why the result keeps the index and name and ends up as `object` dtype with no `NaN` - How the approach compares with `replace` or a per-element Python function ### Follow-up Questions - The fill step also catches values that were already missing in the input. How would you label genuinely missing input differently from unmapped codes? - How would you make the recoding accept uppercase codes such as `'R'`? - What could go wrong if the input Series has a `category` dtype? - How could you get the default without a separate fill step?

Overview: Write a pandas function that recodes single-letter color codes into full color names and sends every unmatched or missing value to a default label. It tests how Series.map treats values missing from a dictionary, how to fill the gaps in one step, and how to keep the index, name and dtype of the result intact.

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

Implement recode_with_default(s). Given a pandas Series of single-letter color codes, return a new Series recoded with the mapping {'r': 'red', 'g': 'green', 'b': 'blue'}, where every value that does not match the mapping becomes 'unknown'.

import pandas as pd

def recode_with_default(s: pd.Series) -> pd.Series:
    ...

Requirements:

  • Recode 'r' to 'red' , 'g' to 'green' and 'b' to 'blue' .
  • Every other value (including None ) becomes 'unknown' .
  • Preserve the original index and name.
  • The result has object dtype and contains no NaN .

Example:

s = pd.Series(['r', 'g', 'x', 'b', None])
recode_with_default(s)
# 0        red
# 1      green
# 2    unknown
# 3       blue
# 4    unknown
# dtype: object

What a Strong Answer Covers Guidance

  • How the chosen method treats values that are missing from the mapping
  • How None and NaN in the input end up labeled 'unknown'
  • Why the result keeps the index and name and ends up as object dtype with no NaN
  • How the approach compares with replace or a per-element Python function

Follow-up Questions Guidance

  • The fill step also catches values that were already missing in the input. How would you label genuinely missing input differently from unmapped codes?
  • How would you make the recoding accept uppercase codes such as 'R' ?
  • What could go wrong if the input Series has a category dtype?
  • How could you get the default without a separate fill step?