We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Handling Column Name Conflicts

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

Rename Before Merging

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.

Assignment

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: