23. Common pandas Mistakes and Fixes
23.7 Practice, Tip and Close
Why. Mistakes compound at project time (Chapter 16). Fixing them here saves the Blinkit story.
What. One lab that plants bugs on purpose, then heals them.
How.
Chapter practice lab
- Build a 12-row fictional orders frame with comma amounts, one
N/A, mixed city case, and two stores sharing city Pune in a dimension table. - Demonstrate a chained assignment warning/fix; replace with
.loc. - Clean dtypes; log NaNs from coerce.
- Show a bad city-only merge explosion; fix with
store_id+validate="m:1". - Localise two IST timestamps; compute minutes.
- Filter Delivered in {Pune, Nashik} with parentheses; print before/after.
- Run
peek()before exporting CSV.
# Seed for the lab — expand and break it on purpose
import pandas as pd
orders = pd.DataFrame({
"order_id": [f"BLK-{i}" for i in range(1, 7)],
"store_id": ["PUN-01", "PUN-02", "PUN-01", "NAS-01", "NGP-01", "KOL-01"],
"city": ["Pune", "pune", "Pune", "Nashik", "Nagpur", "Kolhapur"],
"amount": ["100", "1,250", "90", "N/A", "80", "110"],
"status": ["Delivered", "Delivered", "Cancelled", "Delivered", "Delivered", "Delivered"],
"customer": ["Ravindra Bagale", "Zoya", "Amir", "Salman", "Raja", "Ravina"],
})
print(orders)
Practice task
After fixing the lab, export outputs/orders_fixed.csv and write three bullets: NaNs found, merge rows before/after, filter before/after.
Ravindra Bagale's Tip
pandas errors shikavtat. Warning ignore naka — to free teacher aahe.
Thodkyaat sangaycha tar (quick recap)
- Use
.loc— avoid chained assignment - Coerce junk to NaN and count it
- Merge on true keys; watch row explosion
- Be explicit about IST vs UTC vs date-only
- Parentheses with
&/|; log filter counts - Debug with shape, NA, dups, indicator, control totals
Samajla ka? Hya teen chapters ni stats, sharing ani pandas bugs tight kele — abhyas regular theva, Jupyter ughadun ek bug/day fix kara.