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

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

28.2 merge how=: left, inner, outer

Why. inner silently drops orders with a bad store_id. Ops often wants those rows visible as NaN so cleaning can fix the export.

What.

how Keeps
left All left rows; missing right → NaN
inner Only matching keys
outer Keys from either side
right All right rows (less common from orders)

How.

Orders left-join stores

  1. Build orders and stores.
  2. orders.merge(stores, on="store_id", how="left").
  3. Find unmatched: city/manager is NaN.
  4. Confirm len(merged) == len(orders) for many-to-one.
  5. Only then aggregate GMV.

Worked example A — Classic Blinkit left join.

import pandas as pd

orders = pd.DataFrame({
    "order_id": ["BLK-001", "BLK-002", "AMN-003", "BLK-004"],
    "store_id": ["PUN-01", "NSK-01", "KOP-01", "PUN-99"],
    "amount_inr": [64, 90, 58, 110],
    "status": ["Delivered", "Delivered", "Cancelled", "Delivered"],
    "customer": ["Ruhi Bagale", "Amir", "Zoya", "Ravina"],
})
stores = pd.DataFrame({
    "store_id": ["PUN-01", "NSK-01", "KOP-01", "SLP-01"],
    "city": ["Pune", "Nashik", "Kolhapur", "Solapur"],
    "manager": ["Ravindra Bagale", "Shraddha Bagale", "Salman", "Raja"],
})

left = orders.merge(stores, on="store_id", how="left")
inner = orders.merge(stores, on="store_id", how="inner")
print(left)
print("orders", len(orders), "left", len(left), "inner", len(inner))
print("unmatched", left.loc[left["city"].isna(), "order_id"].tolist())

What you should see. BLK-004 / PUN-99 unmatched on left; missing from inner.

Worked example B — When inner is OK.

Training notebook that only wants “valid store map” rows — inner is fine if you log how many dropped.

Worked example C — Outer for reconciliation.

outer = orders.merge(stores, on="store_id", how="outer", indicator=True)
print(outer["_merge"].value_counts())

Ravindra Bagale's Tip

Ops cleaning = left. Strict valid-only = inner + log. Silent inner = bug factory.