

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 4
click for more info
Not enough gems
Cost: 6 gems
1: Data Merging
incomplete
2: Inner and Left Joins
incomplete
3: Outer Joins
incomplete
4: Merging on Different Keys
incomplete
5: Merging on Composite Keys
incomplete
6: Handling Column Name Conflicts
incomplete
7: Understanding Cardinality
incomplete
8: Merge Validation
incomplete
9: Finding Unmatched Records
incomplete
10: Multi-Table Joins
incomplete
11: Concatenating DataFrames
incomplete
12: Standardizing Schemas
incomplete
13: Building an Integration Pipeline
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
Sometimes the key columns you want to use in a merge have different names across tables, so a simple on="column" isn't enough. One table might call the key prod_id while another calls it id.
sales = pd.DataFrame({"prod_id": ["CHL-482", "TST-731"], "quantity": [10, 20]})
products = pd.DataFrame(
{"id": ["CHL-482", "TST-731"], "name": ["ChillVault Mini", "ToastForge Pro"]}
)
Use left_on and right_on to tell Pandas which columns should be matched up:
result = pd.merge(sales, products, left_on="prod_id", right_on="id", how="left")
print(result)
# prod_id quantity id name
# 0 CHL-482 10 CHL-482 ChillVault Mini
# 1 TST-731 20 TST-731 ToastForge Pro
Notice that both prod_id and id are in the result! They're redundant, so it's common practice to drop one:
result = result.drop(columns="id")
You'll see this pattern constantly in database work. Real systems are far from guaranteed to use the same column names across tables.
SnackStack's sales data uses transaction_id, while the transaction details table calls the same key id. The weekend sales report needs both tables merged without leaving duplicate key columns behind.
Complete the merge_transactions function. It accepts two DataFrames: sales and transactions. It should: