Explode a list column in pandas and count skill frequencies

Quick Overview

Write a pandas function that turns a column of skill lists into one row per skill and returns a frequency table sorted by count and then by name. It tests explode, handling the missing values that empty lists produce, groupby counting, deterministic multi-key sorting and index resets.

Explode a list column in pandas and count skill frequencies

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `explode_then_count(df)`. The input DataFrame has a `'user'` column (string) and a `'skills'` column where each cell is a list of skill strings. Explode the skills and return the frequency of each skill across all users. The function must: - Explode `'skills'` so each list element becomes its own row. - Drop the `NaN` rows that empty lists produce before counting. - Return exactly the columns `['skill', 'count']`. - Sort by `'count'` descending, then by `'skill'` ascending. - Reset to a default `RangeIndex`. Example: ```python df = pd.DataFrame({ 'user': ['a', 'b', 'c'], 'skills': [['py', 'sql'], ['py'], []], }) explode_then_count(df) # skill count # 0 py 2 # 1 sql 1 ``` ```hint Empty lists Check what `explode` puts in the row for an empty list, and whether that value would show up as a skill when you count. ``` ### Constraints & Assumptions - Each `'skills'` cell is a Python list of strings and may be empty. - The count is raw occurrences: a skill that appears twice in one user's list counts twice. ### What a Strong Answer Covers - Turning the list column into one row per skill and knowing what an empty list becomes after that step - Keeping empty-list rows out of the counts and out of the output - Producing exactly the `skill` and `count` columns with a deterministic order, including ties - Returning a clean `RangeIndex` - Edge cases such as missing cells, repeated skills in one list and an empty input ### Follow-up Questions - How would you count distinct users per skill instead of raw occurrences? - Would you get the same result without `explode`, and when would you pick that approach? - If `'skills'` arrived as a comma-separated string such as `"py, sql"`, what would you change?

Overview: Write a pandas function that turns a column of skill lists into one row per skill and returns a frequency table sorted by count and then by name. It tests explode, handling the missing values that empty lists produce, groupby counting, deterministic multi-key sorting and index resets.

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

Implement explode_then_count(df). The input DataFrame has a 'user' column (string) and a 'skills' column where each cell is a list of skill strings. Explode the skills and return the frequency of each skill across all users.

The function must:

  • Explode 'skills' so each list element becomes its own row.
  • Drop the NaN rows that empty lists produce before counting.
  • Return exactly the columns ['skill', 'count'] .
  • Sort by 'count' descending, then by 'skill' ascending.
  • Reset to a default RangeIndex .

Example:

df = pd.DataFrame({
    'user': ['a', 'b', 'c'],
    'skills': [['py', 'sql'], ['py'], []],
})

explode_then_count(df)
#   skill  count
# 0    py      2
# 1   sql      1

Constraints & Assumptions

  • Each 'skills' cell is a Python list of strings and may be empty.
  • The count is raw occurrences: a skill that appears twice in one user's list counts twice.

What a Strong Answer Covers Guidance

  • Turning the list column into one row per skill and knowing what an empty list becomes after that step
  • Keeping empty-list rows out of the counts and out of the output
  • Producing exactly the skill and count columns with a deterministic order, including ties
  • Returning a clean RangeIndex
  • Edge cases such as missing cells, repeated skills in one list and an empty input

Follow-up Questions Guidance

  • How would you count distinct users per skill instead of raw occurrences?
  • Would you get the same result without explode , and when would you pick that approach?
  • If 'skills' arrived as a comma-separated string such as "py, sql" , what would you change?