5.4.21 · D3 · HinglishScientific Computing (Python)

Worked examplesPandas — Series, DataFrame, indexing, groupby, merge, pivot

5,011 words23 min read↑ Read in English

5.4.21 · D3 · Coding › Scientific Computing (Python) › Pandas — Series, DataFrame, indexing, groupby, merge, pivot


The scenario matrix

Pandas mein har operation ke kuch "edge conditions" hote hain jo output badal dete hain. Neeche full grid hai. Har cell ek scenario class hai; aage ke worked examples us cell ke saath tagged hain jo woh cover karta hai.

Case class Tricky sub-cases Example jo cover karta hai
Alignment (+) fully matched · partially matched · disjoint (all NaN) Ex 1
Indexing slices .loc inclusive vs .iloc exclusive · label ≠ position Ex 2
Boolean masking mask kuch select kare · mask kuch na select kare (empty frame) Ex 3
GroupBy normal groups · ek row wala group · NaN/None key dropped Ex 4
GroupBy multi-agg ek saath kai functions · missing data par count vs size Ex 5
Merge how inner () · left · right · outer () · duplicate keys → row explosion Ex 6, Ex 7
Merge edge cases overlapping non-key columns (suffixes=) · multi-key joins Ex 8
Pivot unique cells (plain pivot) · repeated cells → must aggregate Ex 9
Word problem end-to-end: merge → groupby → pivot on real data Ex 10
Exam twist chained-indexing trap + degenerate empty result Ex 11

Ex 1 — Alignment: matched, partial, disjoint


Ex 2 — .loc vs .iloc jab label ≠ position


Ex 3 — Boolean masking: kuch rows vs koi nahi


Ex 4 — GroupBy ek akele group aur NaN key ke saath

Neeche ki figure is example ke liye puri split-apply-combine journey dikhati hai. Left se right padho: left par hain chaar raw rows (key ke hisaab se colour-coded); arrows har row ko uske bucket mein le jaate hain (magenta box N ke liye, violet box S ke liye); dashed grey arrow dikhata hai woh row jiska key None hai jo throw away ho raha hai; har box ke andar aap collected values aur unka mean computed dekh sakte ho. Takeaway: None ki key kabhi kisi bucket tak nahi pahunchti, aur ek single row wala bucket (S) bilkul valid hai.

Figure — Pandas — Series, DataFrame, indexing, groupby, merge, pivot

Ex 5 — Multi-function agg + count-vs-size on missing data


Ex 6 — Merge: same tables par charon how modes

Neeche ki figure do tables ki id values ka Venn diagram hai. Isse kya padhe: left (magenta) circle L ki ids rakhta hai, right (violet) circle R ki ids rakhta hai; beech ki overlap intersection hai ; poora coloured area union hai . Har how mode bas ek rule hai jo kehta hai kaunsa region rakhna hai — inner overlap rakhta hai, left poora magenta circle rakhta hai, right poora violet circle rakhta hai, outer sab kuch rakhta hai. Neeche orange line charon resulting id-sets list karti hai.

Figure — Pandas — Series, DataFrame, indexing, groupby, merge, pivot

Ex 7 — Merge with duplicate keys: row explosion


Ex 8 — Merge edge cases: colliding column names aur multi-key joins


Ex 9 — Pivot: unique cells vs repeated cells


Ex 10 — Word problem: full pipeline merge → groupby → pivot


Ex 11 — Exam twist: chained-indexing trap + empty result


Active recall

Recall Cover karke answer karo
  • Q: a + c disjoint indices ke saath — error ya all-NaN? → all-NaN, valid Series.
  • Q: .loc[100:300] vs .iloc[0:2] index [100,200,300,400] par? → 3 rows vs 2 rows.
  • Q: None wala groupby key — kahan jaata hai? → NaN mein coerce hota hai aur default mein drop hota hai (dropna=False rakhne ke liye).
  • Q: Group [10, NaN] par count vs size? → count=1 (non-NaN), size=2 (rows).
  • Q: left/right/inner se outer row count? → left + right − inner (inclusion–exclusion).
  • Q: Merge key 2× (L) aur 3× (R) repeat ho? → 2×3 = 6 rows (Cartesian per key).
  • Q: Do tables ek non-key column val share karti hain — dono kaise rakhen? → suffixes=("_L","_R") (defaults _x,_y hain).
  • Q: Row identity (store,month) hai — kaise merge karein? → on=["store","month"] (multi-key join).
  • Q: Pivot mein duplicate (index,column) ho? → pivot ValueError raise karta hai; pivot_table(aggfunc=...) use karo.
  • Q: Empty selection ka .mean()? → NaN.
  • Q: aur ka kya matlab hai? → intersection (dono mein naam) aur union (kisi mein bhi naam).