Validated Many-to-One Left Join of Orders and Customers in Pandas

Quick Overview

Write a pandas function that left-joins an orders table to a customers table, fails loudly when a customer key repeats, and returns the rows sorted by order id with a clean index. It tests merge validation, left join behavior and index handling.

Validated Many-to-One Left Join of Orders and Customers in Pandas

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `validated_join(orders, customers)` that left-merges `orders` (`order_id` int, `customer_id` int) with `customers` (`customer_id` int, `name` str) on `customer_id` and returns the resulting DataFrame. The function must meet these requirements: - Merge with `how='left'` and `validate='many_to_one'`. - A duplicate `customer_id` in `customers` raises `MergeError`. Do not catch it. - The columns are exactly `['order_id', 'customer_id', 'name']`, in that order. - Sort by `order_id` ascending and reset to a fresh `RangeIndex` (drop the old index). Example: ```python orders = pd.DataFrame({'order_id': [3, 1, 2], 'customer_id': [10, 20, 10]}) customers = pd.DataFrame({'customer_id': [10, 20], 'name': ['Ann', 'Bob']}) validated_join(orders, customers) # order_id customer_id name # 0 1 20 Bob # 1 2 10 Ann # 2 3 10 Ann # Duplicate customer_id on the right -> raises MergeError customers_bad = pd.DataFrame({'customer_id': [10, 10], 'name': ['Ann', 'A2']}) validated_join(orders, customers_bad) # pandas.errors.MergeError ``` ```hint Check the index After `sort_values`, look at the index labels the frame still carries and compare them with the index in the expected output. ``` ### What a Strong Answer Covers - A single merge call with the left join and the many-to-one validation, with the `MergeError` left to propagate. - Column selection in the required order. - Sorting by `order_id` and resetting the index without keeping the old labels as a column. - What happens to orders whose `customer_id` has no match, and what happens when the right key repeats. - The cost of the merge and the sort. ### Follow-up Questions - What extra safety does `validate='many_to_one'` give beyond checking uniqueness yourself? - How would rows with a `customer_id` absent from `customers` behave, and how would you turn them into an error? - If `orders` already had a column called `name`, what would the merge return, and how would you handle it?

Overview: Write a pandas function that left-joins an orders table to a customers table, fails loudly when a customer key repeats, and returns the rows sorted by order id with a clean index. It tests merge validation, left join behavior and index handling.

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

Implement validated_join(orders, customers) that left-merges orders (order_id int, customer_id int) with customers (customer_id int, name str) on customer_id and returns the resulting DataFrame.

The function must meet these requirements:

  • Merge with how='left' and validate='many_to_one' .
  • A duplicate customer_id in customers raises MergeError . Do not catch it.
  • The columns are exactly ['order_id', 'customer_id', 'name'] , in that order.
  • Sort by order_id ascending and reset to a fresh RangeIndex (drop the old index).

Example:

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

validated_join(orders, customers)
#    order_id  customer_id name
# 0         1           20  Bob
# 1         2           10  Ann
# 2         3           10  Ann

# Duplicate customer_id on the right -> raises MergeError
customers_bad = pd.DataFrame({'customer_id': [10, 10], 'name': ['Ann', 'A2']})
validated_join(orders, customers_bad)  # pandas.errors.MergeError

What a Strong Answer Covers Guidance

  • A single merge call with the left join and the many-to-one validation, with the MergeError left to propagate.
  • Column selection in the required order.
  • Sorting by order_id and resetting the index without keeping the old labels as a column.
  • What happens to orders whose customer_id has no match, and what happens when the right key repeats.
  • The cost of the merge and the sort.

Follow-up Questions Guidance

  • What extra safety does validate='many_to_one' give beyond checking uniqueness yourself?
  • How would rows with a customer_id absent from customers behave, and how would you turn them into an error?
  • If orders already had a column called name , what would the merge return, and how would you handle it?