23. Common pandas Mistakes and Fixes
23.6 Debugging Checklist
In short: A fixed checklist finds 90% of classroom bugs in five minutes.
Why. When GMV “looks wrong”, random plot tweaks waste time. A fixed checklist finds 90% of classroom bugs in five minutes.
What. Always keep these prints near merges and exports:
shapedtypesisna().sum()duplicated().sum()on business keysmerge(..., indicator=True)["_merge"].value_counts()- control totals:
amount.sum()before vs after
How.
Five-minute debug
- Print
df.shapeafter every major step. print(df.isna().sum().sort_values(ascending=False).head()).print(df["order_id"].duplicated().sum()).- On merges:
indicator=Trueand value-counts_merge. - Compare a hand total for one city (Pune) in Excel vs pandas.
- Only then touch charts.
Worked example A — Helper function.
def peek(df, key="order_id", label="df"):
print(f"== {label} ==")
print("shape", df.shape)
print("dtypes\n", df.dtypes)
print("na\n", df.isna().sum())
if key in df.columns:
print("dup keys", df[key].duplicated().sum())
if "amount" in df.columns and pd.api.types.is_numeric_dtype(df["amount"]):
print("amount sum", df["amount"].sum())
peek(df, label="orders")
Worked example B — Merge audit.
left = pd.DataFrame({"store_id": ["PUN-01", "NAS-01"], "amount": [10, 20]})
right = pd.DataFrame({"store_id": ["PUN-01"], "city": ["Pune"]})
m = left.merge(right, on="store_id", how="left", indicator=True)
print(m["_merge"].value_counts())
print(m.loc[m["_merge"] != "both"])
Worked example C — Story from Rani (Nagpur).
Rani’s cancel rate hit 40% after a “quick filter”. Checklist showed status had mixed case (cancelled vs Cancelled) after a bad export round-trip. str.title() fixed it — not a new chart theme.
Ravindra Bagale's Tip
Debug in a fixed order: check the shape, then missing values, then duplicates, then the merge indicator, and then the total for one city. Before you write the fix, write down the row count you expect, so you know when you are done. Draw the chart only at the very end.
Ravindra Bagale's Tip – मराठी
Debug ठराविक क्रमाने करा: आधी shape, मग missing values, मग duplicates, मग merge indicator आणि शेवटी एका city चा total तपासा. Fix लिहिण्याआधी तुम्हाला अपेक्षित असलेला row count लिहून ठेवा, म्हणजे काम कधी पूर्ण झालं ते कळेल. Chart अगदी शेवटी काढा.
Ravindra Bagale's Tip – हिंदी
Debug एक तय क्रम में करो: पहले shape, फिर missing values, फिर duplicates, फिर merge indicator और आख़िर में एक city का total जाँचो। Fix लिखने से पहले जो row count आप expect करते हो उसे लिख लो, ताकि पता चले कि काम कब पूरा हुआ। Chart बिल्कुल आख़िर में बनाओ।