Pivot Sales Data to a Wide Region-by-Product Table in pandas

Quick Overview

Practice reshaping a long sales table into a wide region-by-product matrix in pandas. The task tests how you aggregate repeated key pairs, fill missing combinations with zero, keep an integer dtype and name both axes correctly.

Pivot Sales Data to a Wide Region-by-Product Table in pandas

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `sales_by_region_product(df)`. The input has columns `['region', 'product', 'amount']` (integer `amount`) and may repeat `('region', 'product')` pairs. Return a wide frame of summed amounts: rows are regions, columns are products. Requirements: - Sum `amount` over duplicate pairs. - Row index = distinct regions, named `'region'`. - Columns axis = distinct products, named `'product'`. - Missing combinations filled with `0` (not `NaN`), all cells integer dtype. Example: ```python df = pd.DataFrame({ 'region': ['N', 'N', 'S', 'N'], 'product': ['A', 'A', 'B', 'B'], 'amount': [10, 5, 7, 3], }) sales_by_region_product(df) # product A B # region # N 15 3 # S 0 7 ``` ```hint Duplicates and gaps A plain reshape expects each `(region, product)` pair to appear once, and a cell that ever holds `NaN` pushes its column to float. Look for a single step that aggregates the repeats and supplies the fill value. ``` ### What a Strong Answer Covers - Why a plain reshape fails on repeated pairs, and how the chosen call aggregates them. - How absent combinations become `0` without the column passing through float. - The axis names on the result: `region` on the index and `product` on the columns. - An explicit dtype check or cast, and what happens to rows with a missing region or product. ### Follow-up Questions - How would you add row and column totals (margins)? - What if you needed both sum and count per combination? - How would you build the same table with `groupby` and `unstack`, and when would you choose that route?

Overview: Practice reshaping a long sales table into a wide region-by-product matrix in pandas. The task tests how you aggregate repeated key pairs, fill missing combinations with zero, keep an integer dtype and name both axes correctly.

Nov 24, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Implement sales_by_region_product(df). The input has columns ['region', 'product', 'amount'] (integer amount) and may repeat ('region', 'product') pairs. Return a wide frame of summed amounts: rows are regions, columns are products.

Requirements:

  • Sum amount over duplicate pairs.
  • Row index = distinct regions, named 'region' .
  • Columns axis = distinct products, named 'product' .
  • Missing combinations filled with 0 (not NaN ), all cells integer dtype.

Example:

df = pd.DataFrame({
    'region':  ['N', 'N', 'S', 'N'],
    'product': ['A', 'A', 'B', 'B'],
    'amount':  [10, 5, 7, 3],
})

sales_by_region_product(df)
# product   A   B
# region
# N        15   3
# S         0   7

What a Strong Answer Covers Guidance

  • Why a plain reshape fails on repeated pairs, and how the chosen call aggregates them.
  • How absent combinations become 0 without the column passing through float.
  • The axis names on the result: region on the index and product on the columns.
  • An explicit dtype check or cast, and what happens to rows with a missing region or product.

Follow-up Questions Guidance

  • How would you add row and column totals (margins)?
  • What if you needed both sum and count per combination?
  • How would you build the same table with groupby and unstack , and when would you choose that route?