5.4.21 · HinglishScientific Computing (Python)

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

1,922 words9 min readRead in English

5.4.21 · Coding › Scientific Computing (Python)


Pandas exist KYUN karta hai?

NumPy fast arrays deta hai, lekin arrays sirf integer position se address hote hain. Real data ke naam hote hain: ek row "customer 1023" hai, ek column "revenue" hai. Pandas ek label axis (the index) add karta hai taaki:

  1. Alignment automatic ho — do Series add karne par match label se hota hai, position se nahi.
  2. Missing data ka ghar hoNaN wahan fill hota hai jahan labels match nahi karte.
  3. Heterogeneous columns saath reh sakein — int, float, string, datetime ek hi table mein.
import pandas as pd
s = pd.Series([10, 20, 30], index=["a", "b", "c"])
df = pd.DataFrame({"x": [1, 2], "y": [3, 4]}, index=["r0", "r1"])

Alignment ACTUALLY kaise kaam karta hai (scratch se derive karo)

Ek Series ko ek partial function samjho. Jab tum s1 + s2 karte ho, pandas element 0 ko element 0 se ADD nahi karta. Woh compute karta hai:


Indexing: teen darwaaze ([], .loc, .iloc)


GroupBy: Split → Apply → Combine

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

Merge: Key ke basis par SQL-style joins


Pivot: Long → Wide reshaping


Common mistakes (Steel-man + fix)


Active recall

Recall Cover karke answer karo
  • Q: Series mein kaun se do arrays hote hain? → values + index.
  • Q: Mismatched indices ke saath a + b NaN se bhara kyun hota hai? → alignment union leta hai, unmatched labels → NaN.
  • Q: Slice endpoints par .loc vs .iloc? → loc inclusive (label), iloc exclusive (position).
  • Q: Groupby ke teen steps? → split, apply, combine.
  • Q: Merge mein default how? → inner.
  • Q: pivot ke bajay pivot_table kab use karna padta hai? → jab (index,column) pairs repeat hon (aggregation ki zaroorat ho).
Recall Feynman: 12-saal ke bachche ko explain karo

Socho ek badi locker chart hai, har locker par ek naam ka tag (the index) hai. Ek Series lockers ka ek column hai; ek DataFrame poori wall hai. Jab tum lockers ke do columns add karte ho, tum unhe naam tag se match karte ho, nahi ki kaun pehla hai — agar ek taraf naam missing hai, woh locker khaali rehta hai (NaN). Groupby bachon ko unki team-shirt ke rang se teams mein sort karna aur har team count karna hai. Merge do charts ko ek shared ID column se chipkana hai, jaise school IDs ko test scores se milana. Pivot ek lambi list ko ek neat grid mein rotate karna hai.


Flashcards

What is a pandas Series structurally?
Ek 1-D labelled array = aligned (values array, index array).
Why does adding two Series with different indices produce NaN?
Pandas labels ke union se align karta hai; unmatched labels ka koi partner nahi hota, isliye result NaN hota hai.
Difference between .loc and .iloc on a slice?
.loc label-based hai aur stop inclusive hai; .iloc position-based hai aur stop exclude karta hai.
What are the three phases of groupby?
Rows ko key se split karo, har group par function apply karo, results ko ek labelled object mein combine karo.
What is the default join type in pd.merge?
inner (sirf woh keys rakhta hai jo dono tables mein present hain).
When must you use pivot_table instead of pivot?
Jab (index, column) combination unique nahi hoti — pivot_table duplicates aggregate karta hai, pivot error raise karta hai.
How do you safely set values on filtered rows?
Ek single .loc call use karo: df.loc[mask, "col"] = value (chained indexing avoid karo).
What does how="outer" return in a merge?
Dono tables ke keys ka union, jahan ek side ka match nahi wahan NaN fill karta hai.
Is df.groupby("k") eager or lazy?
Lazy — yeh ek GroupBy object return karta hai; computation tab hota hai jab aggregation/apply call ki jaati hai.

Connections

  • NumPy — har pandas column ek NumPy array hai; neeche vectorized ops hain.
  • SQL Joins — merge INNER/LEFT/RIGHT/OUTER JOIN semantics mirror karta hai.
  • Split-Apply-Combine — woh general data-analysis pattern jo groupby implement karta hai.
  • Tidy Data — pivot/melt long (tidy) aur wide layouts ke beech convert karte hain.
  • Hash Tables — index label lookup ke liye hashing use karta hai.
  • Missing Data / NaN — alignment NaNs ka main source hai.

Concept Map

glue label onto

indexes

columns share row index

enables

union then reindex

no match yields

accessed via

by label inclusive

by position exclusive

split rows by key

apply then combine

NumPy array
integer position

Index
label axis

Series
1-D labelled

DataFrame
2-D labelled table

Alignment by label

NaN for missing labels

Selectors

.loc label-based

.iloc position-based

GroupBy
split-apply-combine