Build a Pareto 80/20 table in pandas that flags the vital few categories

Quick Overview

Write a pandas function that builds a Pareto 80/20 table from a categorical Series, with counts, percentage shares, a running cumulative share and a flag for the vital few levels. It tests counting with nulls excluded, deterministic tie-breaking, exact output dtypes and handling the boundary row at 80% correctly.

Build a Pareto 80/20 table in pandas that flags the vital few categories

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Implement `pareto_table(s: pd.Series) -> pd.DataFrame`, which runs a Pareto (80/20) analysis on a categorical Series and flags the vital few: the smallest leading set of levels that accounts for at least 80% of the data. The returned frame must follow these rules: - Columns, in this order: `level` (object), `count` (int64), `pct` (float64), `cumulative_pct` (float64), `is_vital_few` (bool). - `pct` is each level's share of the non-null total, times 100. Nulls are excluded from both the counts and the total. `cumulative_pct` is the running sum of `pct`. - Rows are sorted by `count` descending, then `level` ascending, with a fresh `RangeIndex`. - `is_vital_few` is True up to and including the first row with `cumulative_pct >= 80.0` (a row at exactly `80.0` is included) and False after it. ```hint The flag rule Look at the running share just before each row is added. That one number tells you whether the row is still needed to reach 80%. ``` ```hint The exact boundary A set of levels that covers exactly 80% must produce exactly `80.0`. Think about which quantity you accumulate so floating-point rounding cannot leave it a hair below. ``` ### Constraints & Assumptions - The input is a pandas Series of category labels, such as product-defect types, and it may contain nulls. - The result is compared to an expected frame with `pandas.testing.assert_frame_equal`, with a float tolerance on `pct` and `cumulative_pct`. Column order, dtypes and the index must match exactly. - The test Series crosses 80% on a boundary row, so the inclusion rule for that row decides the result. ### What a Strong Answer Covers - Counting levels with nulls excluded from the counts and from the denominator - A deterministic sort that breaks count ties by level, followed by a fresh `RangeIndex` - A flag that includes the row crossing 80% (and a row landing exactly on 80.0) and nothing after it - Floating-point behavior at the 80.0 boundary and the exact output dtypes - Edge cases such as an empty or all-null Series ### Follow-up Questions - How would you handle ties at the 80% boundary, where several levels share the same `count`? - How would you make the threshold configurable (for example 90/10)? - What changes if `s` has a pandas categorical dtype with categories that never occur?

Overview: Write a pandas function that builds a Pareto 80/20 table from a categorical Series, with counts, percentage shares, a running cumulative share and a flag for the vital few levels. It tests counting with nulls excluded, deterministic tie-breaking, exact output dtypes and handling the boundary row at 80% correctly.

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

Implement pareto_table(s: pd.Series) -> pd.DataFrame, which runs a Pareto (80/20) analysis on a categorical Series and flags the vital few: the smallest leading set of levels that accounts for at least 80% of the data.

The returned frame must follow these rules:

  • Columns, in this order: level (object), count (int64), pct (float64), cumulative_pct (float64), is_vital_few (bool).
  • pct is each level's share of the non-null total, times 100. Nulls are excluded from both the counts and the total. cumulative_pct is the running sum of pct .
  • Rows are sorted by count descending, then level ascending, with a fresh RangeIndex .
  • is_vital_few is True up to and including the first row with cumulative_pct >= 80.0 (a row at exactly 80.0 is included) and False after it.

Constraints & Assumptions

  • The input is a pandas Series of category labels, such as product-defect types, and it may contain nulls.
  • The result is compared to an expected frame with pandas.testing.assert_frame_equal , with a float tolerance on pct and cumulative_pct . Column order, dtypes and the index must match exactly.
  • The test Series crosses 80% on a boundary row, so the inclusion rule for that row decides the result.

What a Strong Answer Covers Guidance

  • Counting levels with nulls excluded from the counts and from the denominator
  • A deterministic sort that breaks count ties by level, followed by a fresh RangeIndex
  • A flag that includes the row crossing 80% (and a row landing exactly on 80.0) and nothing after it
  • Floating-point behavior at the 80.0 boundary and the exact output dtypes
  • Edge cases such as an empty or all-null Series

Follow-up Questions Guidance

  • How would you handle ties at the 80% boundary, where several levels share the same count ?
  • How would you make the threshold configurable (for example 90/10)?
  • What changes if s has a pandas categorical dtype with categories that never occur?