Count Distinct Products per Region as a pandas Series

Quick Overview

Write a pandas function that counts how many distinct products each region sells and returns the counts as a Series indexed by region. It tests groupby on a single column, distinct counting with missing values, and the name and order of the returned Series.

Count Distinct Products per Region as a pandas Series

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Write `distinct_products_per_region(df)` that takes a DataFrame with columns `region` and `product` and returns a Series counting the distinct products in each region. The result must meet these requirements: - Series indexed by `region`, named `product`. - Sorted by `region` ascending. For example: ```python df = pd.DataFrame({ 'region': ['East', 'East', 'East', 'West', 'West'], 'product': ['A', 'A', 'B', 'C', 'C'], }) distinct_products_per_region(df) # region # East 2 # West 1 # Name: product, dtype: int64 ``` ```hint Check the count and the name Region East has three rows but two distinct products. Pick an operation that returns 2 for it, and check which name the resulting Series inherits. ``` ### Constraints & Assumptions - `df` has the columns `region` and `product`. - `product` may contain missing values. - The return value is a Series, not a DataFrame. ### What a Strong Answer Covers - Grouping by `region` and counting distinct `product` values within each group. - How the returned Series gets the `region` index and the name `product`. - Why the index comes out sorted ascending, and how to make that explicit. - What happens to missing products and missing regions. ### Follow-up Questions - How does `nunique()` treat `NaN` products? - How would you order the result from most to fewest distinct products, breaking ties by region? - The same product sometimes appears as `"A"`, `"a"` and `"A "`. How would you count those as one product?

Overview: Write a pandas function that counts how many distinct products each region sells and returns the counts as a Series indexed by region. It tests groupby on a single column, distinct counting with missing values, and the name and order of the returned Series.

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

Write distinct_products_per_region(df) that takes a DataFrame with columns region and product and returns a Series counting the distinct products in each region.

The result must meet these requirements:

  • Series indexed by region , named product .
  • Sorted by region ascending.

For example:

df = pd.DataFrame({
    'region':  ['East', 'East', 'East', 'West', 'West'],
    'product': ['A', 'A', 'B', 'C', 'C'],
})

distinct_products_per_region(df)
# region
# East    2
# West    1
# Name: product, dtype: int64

Constraints & Assumptions

  • df has the columns region and product .
  • product may contain missing values.
  • The return value is a Series, not a DataFrame.

What a Strong Answer Covers Guidance

  • Grouping by region and counting distinct product values within each group.
  • How the returned Series gets the region index and the name product .
  • Why the index comes out sorted ascending, and how to make that explicit.
  • What happens to missing products and missing regions.

Follow-up Questions Guidance

  • How does nunique() treat NaN products?
  • How would you order the result from most to fewest distinct products, breaking ties by region?
  • The same product sometimes appears as "A" , "a" and "A " . How would you count those as one product?