28. How to Merge and Join Tables in pandas for Blinkit Data
28.6 Common Mistakes (Blinkit Flavour)
Why. Same bugs as Chapter 23, but with store codes like PUN-01 vs PUN01 and rider IDs with spaces.
What.
- Key typos / strip issues (
"PUN-01 "). - Many-to-many duplicate keys → row explosion → double GMV.
- Merging on city name only (two stores in Pune collide).
- Forgetting
how(default is inner!). - concat when you meant merge (stack vs join).
How.
Hardening
stores["store_id"] = stores["store_id"].astype(str).str.strip().- Prefer stable IDs over display names for keys.
- Always pass
how=explicitly. assert stores["store_id"].is_uniquebefore left-joining orders.- Use concat only when stacking the same grain (monthly files) — Chapter 8.1.
Worked example A — Default how trap.
import pandas as pd
a = pd.DataFrame({"store_id": ["PUN-01", "X"], "amount_inr": [1, 2]})
b = pd.DataFrame({"store_id": ["PUN-01"], "city": ["Pune"]})
print("default", len(a.merge(b, on="store_id"))) # inner -> drops X
print("left", len(a.merge(b, on="store_id", how="left")))
Worked example B — City-only key failure.
Two Pune stores merged on city duplicate every Pune order — use store_id.
Worked example C — concat reminder.
sep = pd.DataFrame({"order_id": ["A"], "city": ["Pune"]})
oct_ = pd.DataFrame({"order_id": ["B"], "city": ["Nashik"]})
print(pd.concat([sep, oct_], ignore_index=True)) # stack months — not a store join
Ravindra Bagale's Tip
how= explicit. strip keys. unique dimension. GMV double = explosion alarm.