Build a categorical frequency table with counts, percentages and cumulative percentages in pandas

Quick Overview

Write a pandas function that turns a categorical Series into a frequency table with counts, percentages and cumulative percentages. It tests excluding missing values from the denominator, deterministic tie-breaking, computing a running total after sorting, and exact column dtypes.

Build a categorical frequency table with counts, percentages and cumulative percentages in pandas

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `frequency_table(s: pd.Series) -> pd.DataFrame` that builds a frequency table for a categorical Series with counts, percentages and cumulative percentages. Requirements: - Columns in this order: `level` (object), `count` (int64), `pct` (float64, `count / total * 100`) and `cumulative_pct` (float64, the running total of `pct`). - Exclude NaN from the table and from the denominator. - Sort by `count` descending, then `level` ascending, then `reset_index(drop=True)`. - Do not round `pct` or `cumulative_pct`. They are compared with a float tolerance. Example: ```python s = pd.Series(["billing", "login", "billing", "login", "other", None]) frequency_table(s) # level count pct cumulative_pct # 0 billing 2 40.0 40.0 # 1 login 2 40.0 80.0 # 2 other 1 20.0 100.0 ``` ```hint Ties `value_counts` orders levels by count, but the order among equal counts is not something to rely on. ``` ### Constraints & Assumptions - The grader uses a fixed Series of about 12 category strings with at least one tie in counts and at least one NaN, and compares the result with `assert_frame_equal`. - Levels are strings, so they sort alphabetically. ### What a Strong Answer Covers - Excluding NaN from both the levels and the denominator - Making the tie-break on `level` explicit so the output is deterministic - Computing the running total after sorting so it accumulates in row order - Returning the exact columns, column order and dtypes with a fresh RangeIndex ### Follow-up Questions - How would you include NaN as its own level instead of dropping it? - How would you add a threshold that groups rare levels into an "Other" bucket? - What changes if `s` has a pandas `category` dtype with categories that never occur in the data?

Overview: Write a pandas function that turns a categorical Series into a frequency table with counts, percentages and cumulative percentages. It tests excluding missing values from the denominator, deterministic tie-breaking, computing a running total after sorting, and exact column dtypes.

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

Implement frequency_table(s: pd.Series) -> pd.DataFrame that builds a frequency table for a categorical Series with counts, percentages and cumulative percentages.

Requirements:

  • Columns in this order: level (object), count (int64), pct (float64, count / total * 100 ) and cumulative_pct (float64, the running total of pct ).
  • Exclude NaN from the table and from the denominator.
  • Sort by count descending, then level ascending, then reset_index(drop=True) .
  • Do not round pct or cumulative_pct . They are compared with a float tolerance.

Example:

s = pd.Series(["billing", "login", "billing", "login", "other", None])
frequency_table(s)
#      level  count    pct  cumulative_pct
# 0  billing      2   40.0            40.0
# 1    login      2   40.0            80.0
# 2    other      1   20.0           100.0

Constraints & Assumptions

  • The grader uses a fixed Series of about 12 category strings with at least one tie in counts and at least one NaN, and compares the result with assert_frame_equal .
  • Levels are strings, so they sort alphabetically.

What a Strong Answer Covers Guidance

  • Excluding NaN from both the levels and the denominator
  • Making the tie-break on level explicit so the output is deterministic
  • Computing the running total after sorting so it accumulates in row order
  • Returning the exact columns, column order and dtypes with a fresh RangeIndex

Follow-up Questions Guidance

  • How would you include NaN as its own level instead of dropping it?
  • How would you add a threshold that groups rare levels into an "Other" bucket?
  • What changes if s has a pandas category dtype with categories that never occur in the data?