

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 5
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
When two DataFrames share a column name that isn't the merge key, Pandas needs to distinguish them in the result. By default, it appends _x and _y suffixes, which technically works... but is frankly just slop of the non-ai variety.
sales = pd.DataFrame(
{
"product_id": ["CHL-482", "TST-731"],
"price": [10, 20], # Sale price
}
)
products = pd.DataFrame(
{
"product_id": ["CHL-482", "TST-731"],
"price": [12, 25], # List price
}
)
result = pd.merge(sales, products, on="product_id", how="left")
print(result)
# product_id price_x price_y
# 0 CHL-482 10 12
# 1 TST-731 20 25
Instead, use the suffixes parameter to replace _x and _y with something descriptive:
result = pd.merge(
sales, products, on="product_id", how="left", suffixes=("_sale", "_list")
)
print(result)
# product_id price_sale price_list
# 0 CHL-482 10 12
# 1 TST-731 20 25
When you know ahead of time that there are going to be column name conflicts, it's even better to rename them before doing the merge:
products_renamed = products.rename(columns={"price": "list_price"})
# No conflict: 'price' and 'list_price' are already distinct
result = pd.merge(sales, products_renamed, on="product_id", how="left")
print(result)
# product_id price list_price
# 0 CHL-482 10 12
# 1 TST-731 20 25
The merge call stays clean, and the resulting columns have unambiguous, self-documenting names.
SnackStack's fulfillment team needs to track toaster orders and shipments. Both tables have a status column, so the merged report needs clear column names. Orders that haven't shipped yet should also have an explicit shipment status instead of a missing value.
Complete the merge_with_suffixes function. It accepts two DataFrames: orders and shipments. It should: