Count orders per customer in pandas with groupby().size()

Quick Overview

Count the orders for each customer in a pandas DataFrame and return a named Series indexed by customer ID in ascending order. Tests groupby row counting, how missing values affect different count methods, and shaping the output Series.

Count orders per customer in pandas with groupby().size()

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Write `orders_per_customer(df)` that takes a DataFrame with columns `customer_id` and `order_id` (one row per order) and returns a Series of the row count per `customer_id`. Requirements: - Count every row with `.size()`, including rows where `order_id` is `NaN`. - Series indexed by `customer_id`, named `order_count`. - Index sorted ascending. Example: ```python df = pd.DataFrame({ 'customer_id': [1, 1, 2, 3, 3], 'order_id': [10, 11, 12, 13, None], }) orders_per_customer(df) # customer_id # 1 2 # 2 1 # 3 2 # Name: order_count, dtype: int64 ``` ```hint Check the output shape Look at the name and index that a grouped `size()` comes back with, and whether `groupby` already sorts the group keys. ``` ### What a Strong Answer Covers - Grouping by `customer_id` and counting rows per group, including the row with a missing `order_id`. - A Series named `order_count` with `customer_id` as its index. - How the ascending index order is guaranteed. - How `size()` and `count()` differ on this data. ### Follow-up Questions - When would `count()` give a different result here? - How would you return the result as a DataFrame instead? - What happens to rows where `customer_id` itself is missing?

Overview: Count the orders for each customer in a pandas DataFrame and return a named Series indexed by customer ID in ascending order. Tests groupby row counting, how missing values affect different count methods, and shaping the output Series.

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

Write orders_per_customer(df) that takes a DataFrame with columns customer_id and order_id (one row per order) and returns a Series of the row count per customer_id.

Requirements:

  • Count every row with .size() , including rows where order_id is NaN .
  • Series indexed by customer_id , named order_count .
  • Index sorted ascending.

Example:

df = pd.DataFrame({
    'customer_id': [1, 1, 2, 3, 3],
    'order_id':    [10, 11, 12, 13, None],
})

orders_per_customer(df)
# customer_id
# 1    2
# 2    1
# 3    2
# Name: order_count, dtype: int64

What a Strong Answer Covers Guidance

  • Grouping by customer_id and counting rows per group, including the row with a missing order_id .
  • A Series named order_count with customer_id as its index.
  • How the ascending index order is guaranteed.
  • How size() and count() differ on this data.

Follow-up Questions Guidance

  • When would count() give a different result here?
  • How would you return the result as a DataFrame instead?
  • What happens to rows where customer_id itself is missing?