Sum sales per observed category in pandas, skipping unused categorical levels

Quick Overview

Write a pandas function that totals sales per category for a categorical column whose dtype declares categories that never occur in the data. It tests how groupby treats categorical keys, how the result keeps the categorical order, and how grouping on several categorical columns can blow up into empty groups.

Sum sales per observed category in pandas, skipping unused categorical levels

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `sales_by_category(df)`. `df` has a `category`-dtype column named `category` and a numeric `sales` column. The column's dtype declares some categories that never occur in the data. Return the total `sales` per observed category, ordered by the categorical order. The function must: - Aggregate with a single `groupby` on `category` and a sum over `sales`. - Return no rows (including zero-valued rows) for categories absent from the data. - Return a Series named `sales`, indexed by `category`. Example: ```python cats = pd.Categorical( ['a', 'b', 'a'], categories=['a', 'b', 'c'] # 'c' never appears ) df = pd.DataFrame({'category': cats, 'sales': [10, 5, 3]}) sales_by_category(df) # category # a 13 # b 5 # Name: sales, dtype: int64 ('c' is omitted) ``` ```hint Look at the groupby arguments A categorical grouper can create groups for values that no row contains. Check which `groupby` argument decides whether declared but unseen categories get a group. ``` ### What a Strong Answer Covers - Why grouping on a categorical column can produce rows for categories that never occur, and which argument controls it. - How the result keeps the categorical order and ends up as a Series named `sales` indexed by `category`. - The time and space cost of the aggregation. - Edge cases: missing category values, missing sales values, an empty frame, and an index dtype that still lists the unused categories. ### Follow-up Questions - What changes if you pass `observed=False`, and which aggregations return 0 or NaN for the empty groups? - How does the result behave when you group by two categorical columns, and why does that matter for memory? - A report later needs every declared category, with 0 for the ones that have no sales. How would you produce it? - What happens to rows whose `category` value is missing?

Overview: Write a pandas function that totals sales per category for a categorical column whose dtype declares categories that never occur in the data. It tests how groupby treats categorical keys, how the result keeps the categorical order, and how grouping on several categorical columns can blow up into empty groups.

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

Implement sales_by_category(df). df has a category-dtype column named category and a numeric sales column. The column's dtype declares some categories that never occur in the data. Return the total sales per observed category, ordered by the categorical order.

The function must:

  • Aggregate with a single groupby on category and a sum over sales .
  • Return no rows (including zero-valued rows) for categories absent from the data.
  • Return a Series named sales , indexed by category .

Example:

cats = pd.Categorical(
    ['a', 'b', 'a'],
    categories=['a', 'b', 'c']  # 'c' never appears
)
df = pd.DataFrame({'category': cats, 'sales': [10, 5, 3]})

sales_by_category(df)
# category
# a    13
# b     5
# Name: sales, dtype: int64   ('c' is omitted)

What a Strong Answer Covers Guidance

  • Why grouping on a categorical column can produce rows for categories that never occur, and which argument controls it.
  • How the result keeps the categorical order and ends up as a Series named sales indexed by category .
  • The time and space cost of the aggregation.
  • Edge cases: missing category values, missing sales values, an empty frame, and an index dtype that still lists the unused categories.

Follow-up Questions Guidance

  • What changes if you pass observed=False , and which aggregations return 0 or NaN for the empty groups?
  • How does the result behave when you group by two categorical columns, and why does that matter for memory?
  • A report later needs every declared category, with 0 for the ones that have no sales. How would you produce it?
  • What happens to rows whose category value is missing?