Consolidate Rare Categories to 'Other' by Count in a pandas Series

Quick Overview

Write a pandas function that replaces every category appearing fewer than 3 times in a Series with the label Other while keeping the index, name and object dtype. It tests frequency counting with value_counts, a strict count cutoff and vectorized masking that leaves the input unchanged.

Consolidate Rare Categories to 'Other' by Count in a pandas Series

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `def consolidate_rare(s: pd.Series) -> pd.Series`. Given a Series of string categories, return a new Series where any category with a total count strictly less than 3 is replaced by `'Other'`. Requirements: - Values whose category appears fewer than 3 times in the whole Series become `'Other'`. Values whose category appears 3 or more times are unchanged. - Preserve the original `index` and `name`, with `object` dtype. - Do not mutate the input. - All values are strings. NaN handling is out of scope. Example: ```python s = pd.Series(['a','a','a','b','b','c']) consolidate_rare(s) # 0 a # 1 a # 2 a # 3 Other # 'b' occurs twice # 4 Other # 5 Other # 'c' occurs once # dtype: object ``` `a` appears exactly 3 times, so it stays. `b` (2 times) and `c` (once) fall below the cutoff and both become `'Other'`. ```hint Decide per category, apply per row The rare/frequent decision belongs to a category, using its count over the whole Series. Work out that decision once for each distinct value, then turn it into a mask over the rows. ``` ### What a Strong Answer Covers - Counts taken over the entire Series, so every occurrence of a category gets the same decision. - The strict cutoff: a category with exactly 3 occurrences is kept. - A vectorized replacement that returns a new Series and leaves the input untouched. - The output contract: original index and name, `object` dtype. - Time and space complexity in terms of the number of rows and the number of distinct categories. ### Follow-up Questions - How would you make the threshold and replacement label configurable? - What changes if the input is a `category` dtype Series? - If NaN values came into scope, what would your function do with them, and is that what you want? - The counts were computed on training data. How do you apply the same consolidation to new data?

Overview: Write a pandas function that replaces every category appearing fewer than 3 times in a Series with the label Other while keeping the index, name and object dtype. It tests frequency counting with value_counts, a strict count cutoff and vectorized masking that leaves the input unchanged.

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

Implement def consolidate_rare(s: pd.Series) -> pd.Series. Given a Series of string categories, return a new Series where any category with a total count strictly less than 3 is replaced by 'Other'.

Requirements:

  • Values whose category appears fewer than 3 times in the whole Series become 'Other' . Values whose category appears 3 or more times are unchanged.
  • Preserve the original index and name , with object dtype.
  • Do not mutate the input.
  • All values are strings. NaN handling is out of scope.

Example:

s = pd.Series(['a','a','a','b','b','c'])
consolidate_rare(s)
# 0        a
# 1        a
# 2        a
# 3    Other   # 'b' occurs twice
# 4    Other
# 5    Other   # 'c' occurs once
# dtype: object

a appears exactly 3 times, so it stays. b (2 times) and c (once) fall below the cutoff and both become 'Other'.

What a Strong Answer Covers Guidance

  • Counts taken over the entire Series, so every occurrence of a category gets the same decision.
  • The strict cutoff: a category with exactly 3 occurrences is kept.
  • A vectorized replacement that returns a new Series and leaves the input untouched.
  • The output contract: original index and name, object dtype.
  • Time and space complexity in terms of the number of rows and the number of distinct categories.

Follow-up Questions Guidance

  • How would you make the threshold and replacement label configurable?
  • What changes if the input is a category dtype Series?
  • If NaN values came into scope, what would your function do with them, and is that what you want?
  • The counts were computed on training data. How do you apply the same consolidation to new data?