How to Handle Missing Values in pandas
Read this guide in: English · मराठी · हिंदी
Missing values show up as NaN. First detect them, then choose dropna or fillna, then re-check totals. Netflix-style example: watches 100, NaN, 90 → after fillna(0) the sum is 190.
Chala mitrano! Empty cells break naive sums and confuse charts. Today we clean missing watches with honest rules — not magic. Samajla ka? We re-check at the end.
Quick answer
Vertical cleaning path:
df.isna().sum()— count missing per column.- Inspect rows:
df[df["watches"].isna()] - Decide: drop the row, or fill with 0 / median / labels.
- Apply
dropnaorfillna. - Re-run
info()and a known sum. - Write one notebook note explaining the choice.
detect → decide → dropna/fillna → verify → document
Language: English · मराठी · हिंदी
What do I need before this guide?
- A DataFrame with at least one messy column.
- Course: Types, missing, duplicates.
Before and after (look at the tables first)
Before — Netflix-style watches with a NaN.
After — fillna(0); watches total 190.
Before: Show B is NaN. After: filled with 0; total watches 190.
Why missing values happen
- Export left a blank cell.
- API skipped a field.
- Someone typed spaces instead of a number.
- Join/merge created gaps.
Why you care: a chart can plot NaNs as gaps or drop points silently. Managers then argue with ghosts.
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()
What you see: watches missing count 1.
Detect (do this every time)
Vertical list:
df.isna().sum()df.isna().mean()— share missing (optional).df.info()— non-null counts vs rows.- Spot-check with a filter on
isna().
Course messy sample context: Messy sample.
Decide: drop or fill?
dropna — when it is honest
- The whole row is useless without that field.
- Missingness is rare.
- You document how many rows you removed.
clean = df.dropna(subset=["watches"])
fillna — when zero (or another value) is honest
- For counts,
0often means “no watches recorded”. - For categories, a label like
"unknown"can be clearer than silence. - For some numeric EDA, median fill is a choice — say so in notes.
df["watches"] = df["watches"].fillna(0)
print(df["watches"].sum()) # 190
Shop sales blank cell
Suppose Pune sales blank:
- If the row is a real sale with unknown amount — do not silently fill 0 without asking the business.
- If the export uses blank for “no sale line” — maybe the row should be dropped.
- Cleaning is a business conversation wearing Python clothes.
Rename / strip first (often)
Missing work sits next to messy headers:
- Strip column names.
- Standardise
"N/A","na",""into real NaN.
Course: Rename, strip, standardise.
df["city"] = df["city"].astype(str).str.strip()
df["watches"] = pd.to_numeric(df["watches"], errors="coerce")
errors="coerce" turns junk into NaN — then you handle NaN on purpose.
Duplicates (neighbour topic)
- Missing and duplicates are different diseases.
- Same cleaning chapter covers both — treat them as separate decisions.
- After fillna, check
df.duplicated().sum().
What you see after a good clean
isna().sum()is zero on columns you claim are complete.- Totals match the rule you chose (190 with fill 0 on the sample).
- A Markdown cell explains the rule in one sentence.
Outliers (do not confuse with NaN)
- An outlier is a surprising number that exists.
- NaN is a hole.
- Course: Outliers — after missing values.
Ravindra Bagale's Tip
Never “fix everything with the mean” on autopilot. For watch counts, zero can be honest; for salary surveys, mean fill can lie. Write the rule beside the code. Samajla ka?
Practice task
- Recreate the three-row watches table with one NaN.
- Print
isna().sum(). fillna(0)and prove sum 190.- Make a second copy that
dropnainstead; note the new sum 190 wait — 100+90=190 also — write why both totals match here but would not if Show B had been 50.
Samajla ka? Detect NaN, choose drop or fill, verify the total, write the rule. Then EDA. Aata pudhe jaauya.
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.