Ravindra BagaleCourses & study guides मराठी Track your progress

28. How to Merge and Join Tables in pandas for Blinkit Data

Data Science

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

  1. stores["store_id"] = stores["store_id"].astype(str).str.strip().
  2. Prefer stable IDs over display names for keys.
  3. Always pass how= explicitly.
  4. assert stores["store_id"].is_unique before left-joining orders.
  5. 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.