23. Common pandas Mistakes and Fixes
23.3 Merge Row Explosion (Many-to-Many)
Why. You merge orders to stores on city alone. Pune has 3 stores and 100 orders → 300 rows. GMV triples. Ravindra’s dashboard “goes viral” for the wrong reason.
What.
- One-to-one / many-to-one on a true key keeps grain stable.
- Many-to-many duplicates rows when both sides repeat keys.
validate=andindicator=Truecatch this early.
How.
Merge safely
- Write the grain: “one row = one order”.
- Choose a key that matches that grain (
store_id, not onlycity). df.merge(..., how="left", validate="m:1")when the right table should be unique on the key.- Compare
len(left)vslen(result)immediately. - Use
indicator=Trueto seeleft_only/both/right_only.
Worked example A — Explosion demo.
orders = pd.DataFrame({
"order_id": ["BLK-20", "BLK-21", "BLK-22"],
"city": ["Pune", "Pune", "Nashik"],
"amount": [100, 120, 90],
})
stores = pd.DataFrame({
"city": ["Pune", "Pune", "Nashik"],
"store_id": ["PUN-01", "PUN-02", "NAS-01"],
})
boom = orders.merge(stores, on="city", how="left")
print("orders", len(orders), "after merge", len(boom))
print(boom)
What you should see. 3 → 5 rows. Pune orders duplicated per store.
Worked example B — Fix with store_id.
orders2 = pd.DataFrame({
"order_id": ["BLK-20", "BLK-21", "BLK-22"],
"store_id": ["PUN-01", "PUN-02", "NAS-01"],
"amount": [100, 120, 90],
})
stores2 = pd.DataFrame({
"store_id": ["PUN-01", "PUN-02", "NAS-01"],
"city": ["Pune", "Pune", "Nashik"],
"manager": ["Ruhi Bagale", "Amir", "Zoya"],
})
ok = orders2.merge(stores2, on="store_id", how="left", validate="m:1")
print(len(orders2), len(ok))
print(ok)
Worked example C — indicator report.
orphan = pd.DataFrame({
"order_id": ["BLK-99"],
"store_id": ["UNK-09"],
"amount": [50],
})
chk = orphan.merge(stores2, on="store_id", how="left", indicator=True)
print(chk[["order_id", "store_id", "_merge"]])
left_only means the store master is incomplete — fix the dimension table, do not “fill city with Pune” silently.
Ravindra Bagale's Tip
Print len(df) before and after every merge. If the row count suddenly jumps or revenue doubles, you almost always have duplicate keys. Check the keys with value_counts() before you join, and make validate='m:1' a habit.
Ravindra Bagale's Tip – मराठी
प्रत्येक merge च्या आधी आणि नंतर len(df) print करा. Row count अचानक वाढला किंवा revenue दुप्पट झाला, तर बहुतेक वेळा keys duplicate असतात. Join करण्याआधी value_counts() ने keys तपासा, आणि validate='m:1' वापरण्याची सवय लावा.
Ravindra Bagale's Tip – हिंदी
हर merge से पहले और बाद में len(df) print करो। अगर row count अचानक बढ़ जाए या revenue दोगुना हो जाए, तो ज़्यादातर keys duplicate होती हैं। Join से पहले value_counts() से keys जाँचो, और validate='m:1' लगाने की आदत बनाओ।