28. How to Merge and Join Tables in pandas for Blinkit Data
28.4 Validate: shape, indicator, anti-join
Why. A merge that doubles rows looks “successful” until GMV is 2×. Validation is part of the join — not optional homework.
What.
- Compare
lenbefore/after. indicator=Trueadds_mergecolumn:both,left_only,right_only.- Anti-join — rows on the left with no match: cleaning list for bad keys.
duplicated()on dimension keys before merge (Chapter 23).
How.
Validation pack
stores["store_id"].duplicated().sum()→ must be 0 for many-to-one.- Merge with
indicator=True. value_countson_merge.- Export
left_onlykeys for the data-engineering chat. - Only after that, trust groupby GMV.
Worked example A — indicator.
import pandas as pd
orders = pd.DataFrame({
"order_id": ["BLK-001", "BLK-002", "BLK-004"],
"store_id": ["PUN-01", "NSK-01", "PUN-99"],
"amount_inr": [64, 90, 110],
})
stores = pd.DataFrame({
"store_id": ["PUN-01", "NSK-01", "KOP-01"],
"city": ["Pune", "Nashik", "Kolhapur"],
})
m = orders.merge(stores, on="store_id", how="left", indicator=True)
print(m["_merge"].value_counts())
anti = m.loc[m["_merge"] == "left_only", ["order_id", "store_id"]]
print(anti)
Worked example B — Duplicate dimension key danger.
bad_stores = pd.DataFrame({
"store_id": ["PUN-01", "PUN-01"],
"city": ["Pune", "Pune"],
"manager": ["Ravindra", "Shraddha"],
})
print("dupes", bad_stores["store_id"].duplicated().sum())
exploded = orders.merge(bad_stores, on="store_id", how="left")
print(len(orders), len(exploded)) # explosion if orders hit PUN-01
Worked example C — WhatsApp one-liner.
“Left join orders→stores: 412 orders in, 412 out, 3 left_only store_ids — list attached.”
Ravindra Bagale's Tip
Think of indicator=True as an X-ray of your join, because the _merge column shows which rows matched. Count the left_only rows every time, since those unmatched keys are your next cleaning task. Before you trust any GMV total, check that the dimension key has no duplicates and that the row count did not grow.
Ravindra Bagale's Tip – मराठी
indicator=True ला तुमच्या join चा X-ray समजा, कारण _merge column कोणत्या rows जुळल्या ते दाखवतो. प्रत्येक वेळी left_only rows मोजा, कारण त्या न जुळलेल्या keys हेच तुमचं पुढचं cleaning काम असतं. कोणत्याही GMV total वर विश्वास ठेवण्याआधी dimension key मध्ये duplicates नाहीत आणि row count वाढला नाही ना, ते तपासा.
Ravindra Bagale's Tip – हिंदी
indicator=True को अपने join का X-ray समझो, क्योंकि _merge column दिखाता है कि कौन सी rows match हुईं। हर बार left_only rows गिनो, क्योंकि वही बिना match वाली keys आपका अगला cleaning काम होती हैं। किसी भी GMV total पर भरोसा करने से पहले जाँचो कि dimension key में duplicates नहीं हैं और row count बढ़ा नहीं है।