Ravindra BagaleCourses & study guides Track your progress

Guides

pandas मध्ये missing values कसे handle करायचे

Read this guide in: English · मराठी · हिंदी

Missing values NaN म्हणून दिसतात. आधी detect करा, मग dropna किंवा fillna निवडा, मग totals पुन्हा check. Netflix-style: watches 100, NaN, 90 → fillna(0) नंतर sum 190.

चला मित्रांनो! रिकाम्या cells naive sums तोडतात आणि charts गोंधळतात. आज missing watches प्रामाणिक rules ने clean करूया — जादू नाही. समजलं का? शेवटी पुन्हा check.

थोडक्यात उत्तर

उभा cleaning path:

  1. df.isna().sum() — प्रत्येक column किती missing.
  2. Rows पहा: df[df["watches"].isna()]
  3. Decide: row drop, किंवा 0 / median / labels ने fill.
  4. dropna किंवा fillna लागू करा.
  5. info() आणि known sum पुन्हा चालवा.
  6. Notebook मध्ये choice एका वाक्यात लिहा.
detect → decide → dropna/fillna → verify → document

Language: English · मराठी · हिंदी

या guide आधी काय हवं?

Before आणि after

Before fillna One missing watch count.

Before — Netflix-style watches with a NaN.

After fillna NaN filled with 0; total 190.

After — fillna(0); watches total 190.

Before: Show B = NaN. After: 0 ने fill; total watches 190.

Missing values का येतात?

  1. Export मध्ये blank cell.
  2. API field skip.
  3. Spaces टाइप — number नाही.
  4. Join/merge gaps.

का महत्त्वाचं: chart NaNs gaps म्हणून दाखवू शकतो किंवा points गिळू शकतो. Managers मग भूतांशी भांडतात.

Netflix-style sample

title watches
Show A 100
Show B NaN
Show C 90
import pandas as pd
import numpy as np

df = pd.DataFrame({
    "title": ["Show A", "Show B", "Show C"],
    "watches": [100, np.nan, 90],
})
df.isna().sum()

काय दिसेल: watches missing count 1.

Detect (दर वेळी)

  1. df.isna().sum()
  2. df.isna().mean() — optional share.
  3. df.info() — non-null vs rows.
  4. isna() filter ने spot-check.

Decide: drop की fill?

dropna — प्रामाणिक कधी

  1. त्या field शिवाय row निरुपयोगी.
  2. Missing दुर्मिळ.
  3. किती rows काढल्या ते document.
clean = df.dropna(subset=["watches"])

fillna — 0 (किंवा दुसरी value) प्रामाणिक कधी

  1. Counts साठी 0 = “watches नोंद नाही” असू शकतो.
  2. Categories साठी "unknown" label.
  3. काही numeric EDA मध्ये median — notes मध्ये सांगा.
df["watches"] = df["watches"].fillna(0)
print(df["watches"].sum())  # 190

Shop sales blank

Pune sales रिकामी असेल:

  1. खरी sale पण amount unknown — business न विचारता 0 fill करू नका.
  2. Blank = “sale line नाही” — कदाचित row drop.
  3. Cleaning = Python कपड्यांमधली business conversation.

आधी rename / strip (वारंवार)

df["city"] = df["city"].astype(str).str.strip()
df["watches"] = pd.to_numeric(df["watches"], errors="coerce")

errors="coerce" junk → NaN, मग NaN मुद्दाम handle.

Duplicates (शेजारी topic)

Missing आणि duplicates वेगळे रोग. fillna नंतर df.duplicated().sum() पहा.

Outliers ≠ NaN

Outlier = आश्चर्यकारक पण अस्तित्वात असलेला number. NaN = छिद्र.

Ravindra Bagale's Tip

Autopilot वर “सगळं mean ने fill” करू नका. Watch counts साठी zero प्रामाणिक असू शकतो; salary survey साठी mean fill खोटं बोलू शकतो. Rule code शेजारी लिहा. समजलं का?

Practice task

  1. तीन-row watches + एक NaN.
  2. isna().sum() print.
  3. fillna(0) → sum 190.
  4. दुसरी copy dropna; दोन्ही totals इथे 190 का जुळतात — आणि Show B = 50 असताना का नाही — लिहा.

समजलं का? NaN detect, drop किंवा fill निवडा, total verify, rule लिहा. मग EDA. आता पुढे जाऊया.

Frequently asked questions

What is NaN?

Not a Number — pandas marker for missing numeric/object values in many cases.

Is empty string the same as NaN?

Not always. Strip and replace "" with pd.NA / np.nan during cleaning when needed.

When is dropna wrong?

If most rows miss one optional column, dropping can delete good data — fill or keep and filter later.

fillna mean?

Possible for some numeric EDA; for counts, 0 or “unknown” may be clearer. Be honest in the write-up.

Duplicates too?

Same cleaning chapter covers duplicates — handle missing and dupes as separate decisions.

Course links?

Data cleaning chapter: types, missing, duplicates.