Merge Orders and Customers

Orders contain order_id, customer_id and total. Customers contain unique customer_id values and name. Use a Pandas left merge to keep every order in its original order. Return dictionaries with order_id, customer (the matching name) and total; use "Unknown" for an unmatched customer. Several orders can share a customer. Either input can be empty; empty orders return an empty list.

Difficulty: intermediate

Show hint

Create both DataFrames with their named columns. Merge on customer_id with how="left" and sort=False, fill missing names with "Unknown", rename name to customer, and select the three output columns.

Examples

  • [[{"order_id":2,"customer_id":7,"total":15},{"order_id":1,"customer_id":9,"total":20}],[{"customer_id":7,"name":"Asha"}]][{"order_id":2,"customer":"Asha","total":15},{"order_id":1,"customer":"Unknown","total":20}]

Your solution

Python · Pandas

Results

Python and Pandas load when you Run or Submit. Return a Python list, dictionary or scalar as requested.

Back to Lesson