Consolidate Rare Categories by Proportion in a pandas Series

Quick Overview

Write a pandas function that replaces every category making up less than 10% of the non-null values in a Series with the label Other, keeping nulls, the index and the name. It tests value_counts proportions, strict threshold handling and vectorized masking that leaves the input unchanged.

Consolidate Rare Categories by Proportion in a pandas Series

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Write `consolidate_rare_frac(s: pd.Series) -> pd.Series`. Given a Series of categorical strings, return a new Series where any category whose proportion of the non-null values is strictly less than `0.10` is replaced by `'Other'`. More frequent categories are kept unchanged. Requirements: - A category's proportion is its count divided by the number of non-null values in `s`. - Categories at exactly `0.10` are kept, because the comparison is strict less-than. - Nulls are excluded from the proportions and are left as they are in the output. - Do not mutate the input. Preserve the original `index` and `name`; the result has `object` dtype. - Use vectorized pandas operations, with no Python loop over the values. Examples: ```python s = pd.Series(['a', 'a', 'a', 'a', 'a', 'a', 'a', 'b', 'c', 'd']) # proportions: a=0.7, b=0.1, c=0.1, d=0.1 consolidate_rare_frac(s).tolist() # ['a', 'a', 'a', 'a', 'a', 'a', 'a', 'b', 'c', 'd'] t = pd.Series(['a'] * 10 + ['b', None], name='plan') # 11 non-null values: a=10/11, b=1/11 (about 0.091) consolidate_rare_frac(t).tolist() # ['a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'Other', None] ``` In the first example `b`, `c` and `d` sit exactly at the cutoff, so they survive. In the second, `b` is 1 of 11 non-null values and becomes `'Other'`, while the null stays null. ```hint Count once, then mask Get every category's share in a single pass, then build one boolean mask over the original values. Check what your denominator and your mask each do with nulls. ``` ### What a Strong Answer Covers - Proportions computed over the non-null values only, in one pass over the data. - The strict less-than cutoff, so a category at exactly 0.10 is kept. - A vectorized replacement that leaves nulls in place and returns a new Series while the input stays untouched. - The output contract: original index and name, `object` dtype. - Edge cases such as an empty or all-null Series and an input stored with `category` dtype. ### Follow-up Questions - How would you make the `0.10` threshold a parameter without changing behaviour for existing callers? - How would you handle an all-null or empty Series? - The rare set was computed on training data. How do you apply the same grouping to a test set that contains categories the training data never saw? - What goes wrong if the input Series has `category` dtype, and how do you fix it?

Overview: Write a pandas function that replaces every category making up less than 10% of the non-null values in a Series with the label Other, keeping nulls, the index and the name. It tests value_counts proportions, strict threshold handling and vectorized masking that leaves the input unchanged.

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

Write consolidate_rare_frac(s: pd.Series) -> pd.Series. Given a Series of categorical strings, return a new Series where any category whose proportion of the non-null values is strictly less than 0.10 is replaced by 'Other'. More frequent categories are kept unchanged.

Requirements:

  • A category's proportion is its count divided by the number of non-null values in s .
  • Categories at exactly 0.10 are kept, because the comparison is strict less-than.
  • Nulls are excluded from the proportions and are left as they are in the output.
  • Do not mutate the input. Preserve the original index and name ; the result has object dtype.
  • Use vectorized pandas operations, with no Python loop over the values.

Examples:

s = pd.Series(['a', 'a', 'a', 'a', 'a', 'a', 'a', 'b', 'c', 'd'])
# proportions: a=0.7, b=0.1, c=0.1, d=0.1
consolidate_rare_frac(s).tolist()
# ['a', 'a', 'a', 'a', 'a', 'a', 'a', 'b', 'c', 'd']

t = pd.Series(['a'] * 10 + ['b', None], name='plan')
# 11 non-null values: a=10/11, b=1/11 (about 0.091)
consolidate_rare_frac(t).tolist()
# ['a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'Other', None]

In the first example b, c and d sit exactly at the cutoff, so they survive. In the second, b is 1 of 11 non-null values and becomes 'Other', while the null stays null.

What a Strong Answer Covers Guidance

  • Proportions computed over the non-null values only, in one pass over the data.
  • The strict less-than cutoff, so a category at exactly 0.10 is kept.
  • A vectorized replacement that leaves nulls in place and returns a new Series while the input stays untouched.
  • The output contract: original index and name, object dtype.
  • Edge cases such as an empty or all-null Series and an input stored with category dtype.

Follow-up Questions Guidance

  • How would you make the 0.10 threshold a parameter without changing behaviour for existing callers?
  • How would you handle an all-null or empty Series?
  • The rare set was computed on training data. How do you apply the same grouping to a test set that contains categories the training data never saw?
  • What goes wrong if the input Series has category dtype, and how do you fix it?