Find Customers Without Orders Using a Pandas Left Anti-Join With Indicator

Quick Overview

Write a pandas function that returns the customers who have no orders by running a left merge with indicator=True and keeping the original customer columns and dtypes. It tests anti-join logic, merge fan-out, and how pandas treats null join keys.

Find Customers Without Orders Using a Pandas Left Anti-Join With Indicator

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Implement `customers_without_orders(customers, orders)`, returning the left anti-join: every row in `customers` whose `customer_id` has no match in `orders`. Return a new DataFrame without mutating the inputs. The function must meet these requirements: - Merge on `customer_id` with `how='left', indicator=True`. - Keep rows where `_merge == 'left_only'`. - Drop `_merge` and any order-side columns (for example `order_id`). - Result columns are exactly `['customer_id', 'name']` with the input dtypes (`customer_id` int, `name` str). - Sort by `customer_id` ascending and reset to a fresh `RangeIndex` (drop the old index). Example: ```python customers = pd.DataFrame({ 'customer_id': [1, 2, 3], 'name': ['Ann', 'Bob', 'Cy'], }) orders = pd.DataFrame({ 'order_id': [10, 11], 'customer_id': [1, 1], }) customers_without_orders(customers, orders) # customer_id name # 0 2 Bob # 1 3 Cy ``` Ann has two orders, so she is excluded. Bob and Cy have none. ```hint Watch the intermediate frame Before you filter, check what the left merge does to the row count when one customer has several orders, and which columns it brings along from `orders`. ``` ### What a Strong Answer Covers - How the `_merge` indicator separates customers with a matching order from customers without one. - Whether repeated customer ids in `orders` can change the result, and what they cost in the intermediate frame. - Returning exactly the customer columns with their original dtypes, sorted, with a fresh index, and without touching the inputs. - How null keys behave in a pandas merge and why that needs an explicit decision. ### Follow-up Questions - How would you write the same anti-join without `indicator=True`? - What breaks if `customer_id` contains nulls? - How would you return the customers who do have at least one order, each listed once?

Overview: Write a pandas function that returns the customers who have no orders by running a left merge with indicator=True and keeping the original customer columns and dtypes. It tests anti-join logic, merge fan-out, and how pandas treats null join keys.

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

Implement customers_without_orders(customers, orders), returning the left anti-join: every row in customers whose customer_id has no match in orders. Return a new DataFrame without mutating the inputs.

The function must meet these requirements:

  • Merge on customer_id with how='left', indicator=True .
  • Keep rows where _merge == 'left_only' .
  • Drop _merge and any order-side columns (for example order_id ).
  • Result columns are exactly ['customer_id', 'name'] with the input dtypes ( customer_id int, name str).
  • Sort by customer_id ascending and reset to a fresh RangeIndex (drop the old index).

Example:

customers = pd.DataFrame({
    'customer_id': [1, 2, 3],
    'name': ['Ann', 'Bob', 'Cy'],
})
orders = pd.DataFrame({
    'order_id': [10, 11],
    'customer_id': [1, 1],
})

customers_without_orders(customers, orders)
#    customer_id name
# 0            2  Bob
# 1            3   Cy

Ann has two orders, so she is excluded. Bob and Cy have none.

What a Strong Answer Covers Guidance

  • How the _merge indicator separates customers with a matching order from customers without one.
  • Whether repeated customer ids in orders can change the result, and what they cost in the intermediate frame.
  • Returning exactly the customer columns with their original dtypes, sorted, with a fresh index, and without touching the inputs.
  • How null keys behave in a pandas merge and why that needs an explicit decision.

Follow-up Questions Guidance

  • How would you write the same anti-join without indicator=True ?
  • What breaks if customer_id contains nulls?
  • How would you return the customers who do have at least one order, each listed once?