Ravindra Bagale · Data Scienceसर्व coursesया course चे lessonsशोधाEnglish

Data Science · मराठी आवृत्ती

23.5 खूप rows काढणारे filters

By Ravindra Bagale

या course चे सर्व chapters (py + ch)

हे का शिकायचं?

AND शर्ती जास्त असल्या की data गायब. Bug की खरं filter? Shape log करा.

हे काय आहे?

प्रत्येक filter नंतर len(df) print. श्रेणीशः filters. Negation / NaN व्यवहार लक्षात ठेवा.

कसं करायचं?

Step 1. Start shape लिहा.

Step 2. एक filter → shape.

Step 3. NaN असलेल्या rows कशा वागतात ते पाहा.

Step 4. Final filter business नियमाने जुळतं का.

उदाहरणं: copy करा आणि चालवा

उदाहरण A — Parentheses.

df = pd.DataFrame({
    "city": ["Pune", "Pune", "Nashik", "Nagpur", None],
    "status": ["Delivered", "Cancelled", "Delivered", "Delivered", "Delivered"],
    "amount": [100, 80, 90, 110, 70],
})
# Wrong idea with Python 'and' — will fail or mislead; use &
mask = (df["city"].isin(["Pune", "Nashik"])) & (df["status"] == "Delivered")
print("kept", df.loc[mask])
print("dropped", len(df) - mask.sum())

उदाहरण B — Accidental double filter.

delivered = df.loc[df["status"] == "Delivered"].copy()
# Later someone filters Delivered again on a column already gone — OK
# But if status was overwritten to NaN during cleaning, everything drops:
broken = delivered.copy()
broken["status"] = pd.NA
print("rows left", broken.loc[broken["status"] == "Delivered"].shape[0])

उदाहरण C — isin for six cities.

allowed = [
    "Pune", "Solapur", "Nashik", "Sambhaji Nagar", "Kolhapur", "Nagpur"
]
before = len(df)
df2 = df.loc[df["city"].isin(allowed)].copy()
print(before, "->", len(df2), "excluded", sorted(set(df["city"]) - set(allowed)))

None / NaN cities exit the allow-list — decide fill vs drop explicitly (Chapter 9).