Merge Traps
A merge can surprise you when keys repeat. A duplicated key multiplies rows, and a shared column name gains a suffix.
A duplicated key fans out rows
Pen appears twice in the products table and twice in the orders. The merge gives four Pen rows, not two.
import pandas as pd
orders = pd.DataFrame({
"product": ["Tea", "Pen", "Pen"],
"units": [2, 5, 2],
})
products = pd.DataFrame({
"product": ["Tea", "Pen", "Pen"],
"cost": [2.00, 0.80, 0.80],
})
print(orders.merge(products, on="product"))Shared names gain suffixes
Both tables hold a category column. The merge keeps both, marked _x and _y.
import pandas as pd
orders = pd.DataFrame({
"product": ["Tea", "Coffee"],
"category": ["Drink", "Drink"],
"units": [2, 1],
})
products = pd.DataFrame({
"product": ["Tea", "Coffee"],
"category": ["Drink", "Drink"],
"cost": [2.00, 3.00],
})
merged = orders.merge(products, on="product")
print(list(merged.columns))
print(list(orders.merge(products, on="product", suffixes=("_sale", "_prod")).columns))validate catches the duplicate
validate="many_to_one" expects each product once on the right. The duplicated Pen fails the check, and the error says so.
import pandas as pd
orders = pd.DataFrame({
"product": ["Tea", "Pen", "Pen"],
"units": [2, 5, 2],
})
products = pd.DataFrame({
"product": ["Tea", "Pen", "Pen"],
"cost": [2.00, 0.80, 0.80],
})
try:
orders.merge(products, on="product", validate="many_to_one")
except ValueError as err:
print(err)Example
import io
import pandas as pd
SALES_CSV = """order_id,date,product,category,units,price,region
101,2026-01-05,Tea,Drink,2,4.50,North
102,2026-01-06,Coffee,Drink,1,6.00,South
103,2026-01-07,Mug,Home,3,12.00,East
104,2026-01-08,Notebook,Office,2,8.50,West
105,2026-01-09,Pen,Office,5,2.00,North
106,2026-01-12,Lamp,Home,1,24.00,South
107,2026-01-13,Tea,Drink,4,4.50,East
108,2026-01-14,Coffee,Drink,2,6.00,West
109,2026-01-15,Mug,Home,1,12.00,North
110,2026-01-16,Notebook,Office,3,8.50,South
111,2026-01-19,Pen,Office,2,2.00,East
112,2026-01-20,Lamp,Home,2,24.00,West"""
PRODUCTS_CSV = """product,category,cost
Tea,Drink,2.00
Coffee,Drink,3.00
Mug,Home,6.00
Notebook,Office,4.00
Pen,Office,0.80
Pen,Office,0.80
Lamp,Home,15.00"""
sales = pd.read_csv(io.StringIO(SALES_CSV))
products = pd.read_csv(io.StringIO(PRODUCTS_CSV))
merged = sales.merge(products, on="product")
print(merged.shape)
print(merged[merged["product"] == "Pen"])Key points
- A key that appears twice on one side matches every row on the other side.
- Two matches times two rows gives four rows. This fan-out is the most common merge trap.
- A column name on both tables gains a suffix. The left copy ends in _x and the right copy ends in _y.
- Pass suffixes to merge to pick your own endings.
- Pass validate to merge to check the match. validate="many_to_one" expects each right key once.
In this example, the sales table loads from CSV text, and the products table holds Pen twice. The merge fans out: two Pen orders times two Pen rows gives four Pen rows. The joined table holds 14 rows instead of 12.
Note
validate raises an error when the check fails. The error names the side with the repeated key.
Try it Yourself
Edit the code, then press Run.
Running is simulated · Python Playground
Exercise
The products table lists Pen twice. Two Pen orders merge into how many rows?