Extract every hashtag from a pandas text column with str.extractall

Quick Overview

Write a pandas function that pulls every hashtag out of each string in a Series and returns one row per hashtag under a two-level index. It tests regex capture groups, the shape of a MultiIndex result, and how rows without a match are handled.

Extract every hashtag from a pandas text column with str.extractall

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `extract_all_hashtags(s)`, which pulls every hashtag token from each row of a string `Series` using `s.str.extractall(r'#(\w+)')`. ```python def extract_all_hashtags(s: pd.Series) -> pd.DataFrame: ... ``` The result must meet these requirements: - Each result row is one hashtag token, without the leading `#`. - The result has a `MultiIndex`: the original index on the outer level and the per-row match number on the inner level, named `match`. - The single unnamed group yields the column label `0`. - Rows with no match contribute no entries. Example: ```python s = pd.Series(['#data #ml', 'no tags', '#pandas']) # index [0, 1, 2] extract_all_hashtags(s) # 0 # match # 0 0 data # 1 ml # 2 0 pandas # (index 1 contributes no rows) ``` ```hint What the pattern returns Only the text inside the capture group reaches the result. Run the call on the example and look at `result.index` to see how each match points back to its source row. ``` ### What a Strong Answer Covers - Why one unnamed capture group gives a single column labeled `0`, and how the `#` stays out of the token. - What each level of the `MultiIndex` holds and how it maps a match back to its source row. - What happens to rows with no hashtag and to missing values. - Where `\w+` draws the wrong token boundaries (hyphens, a `#` inside a word or URL) and how the pattern could be tightened. ### Follow-up Questions - How does `extractall` differ from `extract`? - How would you flatten the result to one comma-separated string per original row? - How would you count how often each hashtag appears across the Series, treating `#ML` and `#ml` as the same tag? - How would you change the pattern so a `#` in the middle of a word, as in `page#section`, is not treated as a hashtag?

Overview: Write a pandas function that pulls every hashtag out of each string in a Series and returns one row per hashtag under a two-level index. It tests regex capture groups, the shape of a MultiIndex result, and how rows without a match are handled.

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

Implement extract_all_hashtags(s), which pulls every hashtag token from each row of a string Series using s.str.extractall(r'#(\w+)').

def extract_all_hashtags(s: pd.Series) -> pd.DataFrame:
    ...

The result must meet these requirements:

  • Each result row is one hashtag token, without the leading # .
  • The result has a MultiIndex : the original index on the outer level and the per-row match number on the inner level, named match .
  • The single unnamed group yields the column label 0 .
  • Rows with no match contribute no entries.

Example:

s = pd.Series(['#data #ml', 'no tags', '#pandas'])  # index [0, 1, 2]
extract_all_hashtags(s)

#               0
#   match
# 0 0        data
#   1          ml
# 2 0      pandas
# (index 1 contributes no rows)

What a Strong Answer Covers Guidance

  • Why one unnamed capture group gives a single column labeled 0 , and how the # stays out of the token.
  • What each level of the MultiIndex holds and how it maps a match back to its source row.
  • What happens to rows with no hashtag and to missing values.
  • Where \w+ draws the wrong token boundaries (hyphens, a # inside a word or URL) and how the pattern could be tightened.

Follow-up Questions Guidance

  • How does extractall differ from extract ?
  • How would you flatten the result to one comma-separated string per original row?
  • How would you count how often each hashtag appears across the Series, treating #ML and #ml as the same tag?
  • How would you change the pattern so a # in the middle of a word, as in page#section , is not treated as a hashtag?