Pareto 80/20 Verdict for a Categorical Series

Quick Overview

Write a pandas function that checks whether a categorical Series follows the Pareto 80/20 pattern by finding how many of the most frequent levels cover a target share of rows. It tests frequency ranking with nulls excluded, an inclusive coverage boundary, exact float64 output and sensible handling of empty input.

Pareto 80/20 Verdict for a Categorical Series

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Implement `does_80_20_hold(s: pd.Series, coverage: float = 0.8) -> pd.Series`. Rank the levels of a categorical Series by frequency, descending, and find the smallest set of leading levels whose cumulative row share reaches at least `coverage`. Return the measured values, not a boolean, as a Series named `pareto_verdict` with index name `metric`. The returned Series has these entries, in this order, all `float64`: - `n_levels`: the number of distinct non-null levels. - `n_vital_few`: the number of leading levels needed to reach at least `coverage` of the rows. - `pct_of_levels`: `n_vital_few / n_levels * 100`. - `pct_of_rows`: the actual cumulative row share of those levels, times 100. For example: ```python s = pd.Series(['a']*80 + ['b']*10 + ['c']*5 + ['d']*3 + ['e']*2) does_80_20_hold(s, coverage=0.8) # metric # n_levels 5.0 # n_vital_few 1.0 # pct_of_levels 20.0 # pct_of_rows 80.0 # Name: pareto_verdict, dtype: float64 ``` ```hint Finding the cutoff The cumulative share never decreases as you move down the ranked levels, so the position where it first reaches `coverage` can be found with one comparison over the whole column. ``` ```hint Edge cases Settle two cases before the main path: an empty or all-null Series, and a level whose cumulative share lands exactly on `coverage`. ``` ### Constraints & Assumptions - `s` is a pandas Series of category labels and may contain nulls, which are ignored. - `coverage` is a fraction between 0 and 1. The default 0.8 gives the classic 80/20 check. - The output is compared with `pandas.testing.assert_series_equal` under a float tolerance, so the entry order, dtype, Series name and index name must match. ### What a Strong Answer Covers - Excluding nulls before counting, then ranking levels by frequency - An inclusive boundary: the level that crosses `coverage` counts, and a level landing exactly on it is enough - Reporting the share actually reached, which can exceed `coverage` - The exact output shape: entry order, float64 values, Series name and index name - Edge cases such as an empty Series ### Follow-up Questions - How would you handle ties in frequency at the coverage boundary? - How would you turn the result into a defensible boolean "holds" flag? - How would you also report which levels make up the vital few?

Overview: Write a pandas function that checks whether a categorical Series follows the Pareto 80/20 pattern by finding how many of the most frequent levels cover a target share of rows. It tests frequency ranking with nulls excluded, an inclusive coverage boundary, exact float64 output and sensible handling of empty input.

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

Implement does_80_20_hold(s: pd.Series, coverage: float = 0.8) -> pd.Series. Rank the levels of a categorical Series by frequency, descending, and find the smallest set of leading levels whose cumulative row share reaches at least coverage. Return the measured values, not a boolean, as a Series named pareto_verdict with index name metric.

The returned Series has these entries, in this order, all float64:

  • n_levels : the number of distinct non-null levels.
  • n_vital_few : the number of leading levels needed to reach at least coverage of the rows.
  • pct_of_levels : n_vital_few / n_levels * 100 .
  • pct_of_rows : the actual cumulative row share of those levels, times 100.

For example:

s = pd.Series(['a']*80 + ['b']*10 + ['c']*5 + ['d']*3 + ['e']*2)
does_80_20_hold(s, coverage=0.8)
# metric
# n_levels         5.0
# n_vital_few      1.0
# pct_of_levels   20.0
# pct_of_rows     80.0
# Name: pareto_verdict, dtype: float64

Constraints & Assumptions

  • s is a pandas Series of category labels and may contain nulls, which are ignored.
  • coverage is a fraction between 0 and 1. The default 0.8 gives the classic 80/20 check.
  • The output is compared with pandas.testing.assert_series_equal under a float tolerance, so the entry order, dtype, Series name and index name must match.

What a Strong Answer Covers Guidance

  • Excluding nulls before counting, then ranking levels by frequency
  • An inclusive boundary: the level that crosses coverage counts, and a level landing exactly on it is enough
  • Reporting the share actually reached, which can exceed coverage
  • The exact output shape: entry order, float64 values, Series name and index name
  • Edge cases such as an empty Series

Follow-up Questions Guidance

  • How would you handle ties in frequency at the coverage boundary?
  • How would you turn the result into a defensible boolean "holds" flag?
  • How would you also report which levels make up the vital few?