Skip to content
LakeBench
ProblemsCommunityPricing
Sign inStart practicing

Clean messy text columns

Pandas data engineering interview problem. Difficulty: beginner. Pattern: String Functions. About 12 minutes. Part of the Pro drill bank.

Trim, lowercase and collapse spaces in names, and pull a numeric code out of a reference string. Treat this as a production helper: match the contracted return shape, including empty and duplicate inputs.

df has id, name (messy text) and ref (a reference such as 'INV-0042'). Return: clean_name: name with leading and trailing spaces removed, repeated inner spaces collapsed to a single space, in lowercase code: the first run of digits found in ref as a number ('INV-0042' gives 42), or missing when ref has no digits Columns: id, clean_name, code. Keep the row order, reset the index. A missing name stays missing. Assign the DataFrame to result.

Requirements

  • Missing stays missing.

Constraints

  • Whitespace means spaces and tabs.
  • Codes fit in a normal integer.

Examples

Input: df id | name | ref 1 | Alice SMITH | INV-0042 2 | bob Jones | no code 3 | NULL | A7B912 4 | CY | X-100 Output: id | clean_name | code 1 | alice smith | 42 2 | bob jones | NULL 3 | NULL | 7 4 | cy | 100 Alice Smith loses the padding and the repeated spaces; the tab in bob\tJones counts as whitespace. A7B912 yields the first digit run, 7. The reference without digits has no code.

Topics: lakebench, pandas, str accessor, regex, cleaning.

More interview problems · All interview problems · Learn data engineering

beginner

Clean messy text columns

Interview-style drill: Trim, lowercase and collapse spaces in names, and pull a numeric code out of a reference string.

`df` has `id`, `name` (messy text) and `ref` (a reference such as `'INV-0042'`). Return: - `clean_name`: `name` with leading and trailing spaces removed, repeated inner spaces collapsed to a single space, in lowercase - `code`: the first run of digits found in `ref` as a number (`'INV-0042'` gives `42`), or missing when `ref` has no digits Columns: `id`, `clean_name`, `code`. Keep the row order, reset the index. A missing `name` stays missing. Assign the DataFrame to `result`.