Lesson 2 — Beginner
Pandas DataFrames — Data ka Asli Kaamghar
Data analyst ki 80% zindagi DataFrame me guzarti hai — load karna, saaf karna, filter karna, nikaal dena. Is lesson me hum ek sales CSV uthakar uske saare core operations cover karenge.
DataFrame hai kya?
WHAT
DataFrame = labelled table with rows aur columns — Excel sheet jaisa lekin code se chalta hua. Har column ka apna naam (Series) aur har row ka index hota hai.
WHEN
CSV/Excel/SQL/JSON load karte hi. Cleaning, filtering, group summaries, exports — har data task DataFrame par shuru hota hai.
WHERE
Dashboards se lekar ML feature-building tak. "Pandas aata hai?" ka matlab hai interview me ek 5-minute live task: "is CSV ka top-5 karo".
Load aur first look
import pandas as pd
df = pd.read_csv("sales.csv")
print(df.head(3)) # pehli 3 rows
print(df.shape) # (rows, columns)
print(df.columns) # column names
print(df.dtypes) # har column ka type
print(df.info()) # types + non-null counts
print(df.describe()) # count/mean/std/min/max/percentiles
print(df["city"].value_counts()) # category counts
print(df.sort_values("amount", ascending=False).head())
head → shape → info → describe — yeh 4 line run karo. 2 minute me data ka pura haal pata chal jaata hai: columns kaunse, missing kitne, numbers sahi type me hain ya nahi.loc vs iloc
df = pd.DataFrame(
{"city": ["Lucknow", "Kanpur", "Varanasi", "Lucknow"],
"amount": [4500, 2300, 6100, 1200]},
index=["r1", "r2", "r3", "r4"])
# iloc = INTEGER position (0, 1, 2...)
print(df.iloc[0]) # pehli row
print(df.iloc[0:2, 1]) # rows 0-1, column position 1
# loc = LABEL (index naam se, column naam se)
print(df.loc["r3"]) # row jiska index = r3
print(df.loc["r2", "amount"]) # 2300
print(df.loc[:, "city"]) # poora city column
# filtering (sabse zyada istemal)
big = df[df["amount"] > 2000]
multi = df[(df["amount"] > 1000) & (df["city"] == "Lucknow")]
# naya column
df["tax"] = df["amount"] * 0.18
df["level"] = df["amount"].apply(lambda x: "high" if x > 4000 else "low")
iloc me end-exclusive slicing chalti hai; loc me end-INCLUSIVE (label hai, position nahi). Ye dono confuse karne ka #1 reason hai.Missing values — saaf-safai
print(df.isna().sum()) # column-wise missing count
df = df.dropna() # saari missing wali rows hato
df["amount"].fillna(0, inplace=True) # 0 se bharo
df["city"].fillna(df["city"].mode()[0], inplace=True) # mode se bharo
df["amount"] = df["amount"].fillna(df["amount"].median()) # median (outlier-safe)
# galat types fix
df["date"] = pd.to_datetime(df["date"])
df["amount"] = pd.to_numeric(df["amount"], errors="coerce") # na ban jaata hai
# duplicates
print(df.duplicated().sum())
df = df.drop_duplicates()
Kab kya karein: Missing < 5% aur random ho to dropna chalega; numeric column ho to median (mean se outliers kharab nahi karte); category ho to mode. Hamesha missing ka reason samjho — 0 bhar dena kabhi kabhi ghalat conclusion deta hai.
Export aur chaining
# Method chaining - beech me variable na banao
result = (df[df["amount"] > 1000]
.sort_values("amount", ascending=False)
.head(10))
df.to_csv("clean_sales.csv", index=False)
df.to_excel("report.xlsx", sheet_name="sales")
df.groupby("city")["amount"].sum().to_csv("city_totals.csv")
# DataFrame khol ke dekho (Jupyter)
import matplotlib.pyplot as plt
df["amount"].hist(bins=30)
plt.show()
Exercise
Question: Index LABEL se row nikalne ke liye kaunsa accessor use karte hain? (3 letters)
Question: Missing numbers ko values se bharne ka pandas method? (6 letters)
Common mistakes
- inplace ka andaza:
dropna(inplace=True)chaining rok deta hai — assignment style (df = df.dropna()) cleaner hai. - mean se fill jab outlier ho: salary column me 1 crore wala outlier mean hila deta hai — median use karo.
- SettingWithCopyWarning:
df[df.x>1]["y"] = 0pehli slice pe kaam nahi karta —.loc[mask, "y"] = 0likho. - index ko column na samajhna: export me index chahiye to
index=True, warna hameshaindex=False.
df.shape[1] columns. Ye chhota sawaal screen-round me poochh liya jaata hai.Lesson ho gaya? Ab next pe chalte hain — GroupBy, Merge & Pivot.