You want to merge these to get customer names with their orders, including customers without orders and orders without customers. Which code correctly does this?
hard
A. pd.merge(df_customers, df_orders, left_on='cust_id', right_on='customer', how='inner')
B. pd.merge(df_customers, df_orders, left_on='cust_id', right_on='customer', how='outer')
C. pd.merge(df_customers, df_orders, left_on='name', right_on='customer', how='left')
D. pd.merge(df_customers, df_orders, on='cust_id', how='inner')
Solution
Step 1: Identify columns to join on
Customer IDs are cust_id in df_customers and customer in df_orders. So use left_on='cust_id' and right_on='customer'.
Step 2: Choose join type for all data
To include all customers and all orders (even unmatched), use how='outer' for full outer join.
Final Answer:
pd.merge(df_customers, df_orders, left_on='cust_id', right_on='customer', how='outer') -> Option B
Quick Check:
Outer join + left_on/right_on = all data combined [OK]
Hint: Use how='outer' with left_on/right_on for full merge [OK]
Common Mistakes:
Using inner join loses unmatched rows
Merging on wrong columns
Using name column instead of ID
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.