Explode Comma-Separated Skills to Rows

Quick Overview

Write a pandas function that turns comma-separated skill strings with messy spacing into one row per skill, repeating the id and returning a fresh index. It tests string splitting, expanding list values into rows, whitespace cleanup and edge cases such as empty tokens.

Explode Comma-Separated Skills to Rows

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `def split_skills(df: pd.DataFrame) -> pd.DataFrame`. The input has columns `['id', 'skills']`, where each `skills` cell is a comma-separated string with inconsistent whitespace. Emit one row per skill, duplicating the `id`. Requirements: - Split on `,` and strip leading and trailing whitespace from each token. - Return columns exactly `['id', 'skill']`. - Reset to a fresh default `RangeIndex`. Example: ```text # input id skills 1 python, sql , pandas 2 java # output id skill 1 python 1 sql 1 pandas 2 java ``` ```hint Two steps Turn each cell into a list of tokens first, then find a way to expand a list-valued column into rows while the `id` repeats alongside. Splitting on `', '` will not cope with the spacing in the example. ``` ### Constraints & Assumptions - Every `id` has a non-empty skills string. ### What a Strong Answer Covers - How each string becomes separate rows with the `id` repeated for every token - Cleaning whitespace that can sit on either side of a comma - Returning exactly the `id` and `skill` columns with a fresh `RangeIndex` - How empty tokens and missing cells behave, and what the work costs on a large frame ### Follow-up Questions - How would you drop empty tokens from trailing commas like `'python,,sql'`? - How would you deduplicate skills per `id`? - How would you turn the result into one indicator column per skill for modeling?

Overview: Write a pandas function that turns comma-separated skill strings with messy spacing into one row per skill, repeating the id and returning a fresh index. It tests string splitting, expanding list values into rows, whitespace cleanup and edge cases such as empty tokens.

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

Implement def split_skills(df: pd.DataFrame) -> pd.DataFrame. The input has columns ['id', 'skills'], where each skills cell is a comma-separated string with inconsistent whitespace. Emit one row per skill, duplicating the id.

Requirements:

  • Split on , and strip leading and trailing whitespace from each token.
  • Return columns exactly ['id', 'skill'] .
  • Reset to a fresh default RangeIndex .

Example:

# input
   id             skills
    1  python, sql , pandas
    2                  java

# output
   id   skill
    1  python
    1     sql
    1  pandas
    2    java

Constraints & Assumptions

  • Every id has a non-empty skills string.

What a Strong Answer Covers Guidance

  • How each string becomes separate rows with the id repeated for every token
  • Cleaning whitespace that can sit on either side of a comma
  • Returning exactly the id and skill columns with a fresh RangeIndex
  • How empty tokens and missing cells behave, and what the work costs on a large frame

Follow-up Questions Guidance

  • How would you drop empty tokens from trailing commas like 'python,,sql' ?
  • How would you deduplicate skills per id ?
  • How would you turn the result into one indicator column per skill for modeling?