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
Always write how= yourself, strip spaces from keys and make sure the dimension key is unique before you merge. If store_id is a number on one side and text on the other, the merge fails or nothing matches, so fix the types first. If GMV suddenly doubles, treat it as a row explosion alarm and look for duplicate keys.
Ravindra Bagale's Tip – मराठी
Merge करण्याआधी how= नेहमी स्वतः लिहा, keys मधले spaces काढा (strip करा) आणि dimension key unique आहे याची खात्री करा. store_id एका बाजूला number आणि दुसऱ्या बाजूला text असेल तर merge fail होतो किंवा काहीच जुळत नाही, म्हणून आधी types ठीक करा. GMV अचानक दुप्पट झाला तर तो row explosion चा alarm समजा आणि duplicate keys शोधा.
Ravindra Bagale's Tip – हिंदी
Merge करने से पहले how= हमेशा ख़ुद लिखो, keys से spaces हटाओ (strip करो) और पक्का करो कि dimension key unique है। अगर store_id एक तरफ़ number और दूसरी तरफ़ text है, तो merge fail होता है या कुछ भी match नहीं होता, इसलिए पहले types ठीक करो। अगर GMV अचानक दोगुना हो जाए, तो इसे row explosion का alarm समझो और duplicate keys ढूँढो।