Extract value and unit columns from measurement strings with pandas str.extract

Quick Overview

Use pandas str.extract with named capture groups to split measurement strings such as 10kg or 3.5 L into value and unit columns. It tests regex group design, how extract builds its output columns and index, and which inputs a search-based pattern silently misparses.

Extract value and unit columns from measurement strings with pandas str.extract

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `extract_value_unit(s: pd.Series) -> pd.DataFrame` that splits measurement strings like `'10kg'` into a value column and a unit column using `str.extract` with the named-group pattern `r'(?P<value>\d+(?:\.\d+)?)\s*(?P<unit>[a-zA-Z]+)'`. Requirements: - Return a `DataFrame` with exactly the columns `['value', 'unit']` (taken from the group names). - Keep `value` as a string: do not cast to numeric. - Tolerate optional whitespace between number and unit. Values may be integers or decimals. - Non-matching entries yield `NaN` in both columns. - Preserve the original index of the input `Series`. Example: ```python s = pd.Series(['10kg', '3.5 L', '200 mg', 'n/a']) extract_value_unit(s) # value unit # 0 10 kg # 1 3.5 L # 2 200 mg # 3 NaN NaN ``` ```hint Where the match can start `str.extract` takes the first match found anywhere in each string, the way `re.search` does. Work out what that does to an input such as `'1,000 g'`. ``` ### What a Strong Answer Covers - Uses one vectorized `str.extract` call whose named groups become the `value` and `unit` columns. - Keeps `value` as string data and preserves the input index. - Explains how `\s*` and the optional decimal group handle the example inputs, and why `'n/a'` gives `NaN` in both columns. - Names inputs the pattern misparses because it searches the string instead of matching all of it. ### Follow-up Questions - How would you additionally return `value` cast to a numeric dtype? - How would you normalize unit casing (for example `'L'` vs `'l'`)? - How would you change the pattern so that only strings that are entirely a measurement produce a match?

Overview: Use pandas str.extract with named capture groups to split measurement strings such as 10kg or 3.5 L into value and unit columns. It tests regex group design, how extract builds its output columns and index, and which inputs a search-based pattern silently misparses.

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

Implement extract_value_unit(s: pd.Series) -> pd.DataFrame that splits measurement strings like '10kg' into a value column and a unit column using str.extract with the named-group pattern r'(?P<value>\d+(?:\.\d+)?)\s*(?P<unit>[a-zA-Z]+)'.

Requirements:

  • Return a DataFrame with exactly the columns ['value', 'unit'] (taken from the group names).
  • Keep value as a string: do not cast to numeric.
  • Tolerate optional whitespace between number and unit. Values may be integers or decimals.
  • Non-matching entries yield NaN in both columns.
  • Preserve the original index of the input Series .

Example:

s = pd.Series(['10kg', '3.5 L', '200 mg', 'n/a'])
extract_value_unit(s)
#   value unit
# 0    10   kg
# 1   3.5    L
# 2   200   mg
# 3   NaN  NaN

What a Strong Answer Covers Guidance

  • Uses one vectorized str.extract call whose named groups become the value and unit columns.
  • Keeps value as string data and preserves the input index.
  • Explains how \s* and the optional decimal group handle the example inputs, and why 'n/a' gives NaN in both columns.
  • Names inputs the pattern misparses because it searches the string instead of matching all of it.

Follow-up Questions Guidance

  • How would you additionally return value cast to a numeric dtype?
  • How would you normalize unit casing (for example 'L' vs 'l' )?
  • How would you change the pattern so that only strings that are entirely a measurement produce a match?