Python Central HubSign in

Data Analytics with Python / Phase 3: Data Manipulation with Pandas

Handling Missing Data (isna, fillna, dropna)

Learn how Pandas represents missing values and how to detect, remove, and impute them safely.

Why missing data is normal

Missing values happen because of:

  • Optional form fields (e.g., phone number)
  • Data entry errors
  • Failed joins/merges
  • Incomplete logs

Pandas typically represents missing values as NaN (and sometimes None).

Setup example with missing values

Python
import pandas as pd
import numpy as np
 
df = pd.DataFrame({
    "name": ["Asha", "Ravi", "Meera", None],
    "age": [23, np.nan, 26, 31],
    "city": ["Pune", "Delhi", None, "Pune"],
    "score": [88, 91, np.nan, 95],
})
 
print(df)

Detect missing values

isna() / isnull()

Python
print(df.isna())

Count missing per column

Python
print(df.isna().sum())

Remove missing values: dropna()

Drop rows with any missing values

Python
clean = df.dropna()
print(clean)

Drop rows where a specific column is missing

Python
clean = df.dropna(subset=["score"])
print(clean)

Fill missing values: fillna()

Fill with a constant

Python
filled = df.copy()
filled["city"] = filled["city"].fillna("Unknown")
print(filled)

Fill numeric missing values with mean/median

Python
filled = df.copy()
filled["score"] = filled["score"].fillna(filled["score"].median())
print(filled)

Forward fill / backward fill

Useful for time series or repeated categories.

Python
filled = df.copy()
filled["city"] = filled["city"].ffill()
print(filled)

Important: be explicit

Before deciding how to handle missing data, ask:

  • Is missingness random or meaningful?
  • Should missing values be removed or imputed?
  • Will filling change the meaning of the data?

In analytics, documenting missing-data decisions is part of good practice.

Visualize it

diagram Two ways to handle missing values mermaid
After detecting missing values, you either drop the affected rows/columns or fill them in.

๐Ÿงช Try It Yourself

Exercise 1 โ€“ Detect Missing Values

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 2 โ€“ Drop Rows With Any Missing Value

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 3 โ€“ Fill Missing Values With the Mean

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Check your understanding

quizCheck yourself
  1. What does df.isna().sum() return?

    Show answer

    A โ€” The number of missing values in each column โ€” isna() marks missing cells as True and sum() adds them up per column.

  2. In the sample data, only Asha's row has no missing values. What does df.dropna() return?

    Show answer

    B โ€” Just 1 row โ€” dropna() removes every row with any missing value, and Ravi, Meera and the row with name None each have at least one.

  3. What does df.dropna(subset=["score"]) do?

    Show answer

    A โ€” Drops rows where score is missing โ€” subset limits the check to the listed columns, so only Meera's row (score NaN) is removed.

  4. Why fill missing scores with the median instead of 0?

    Show answer

    A โ€” The median keeps the typical level of the data, while 0 would distort it โ€” For scores 88, 91 and 95 the median is 91, a realistic fill. Filling with 0 would drag down averages and change the meaning of the data.

Next

With missing values under control, move on to Cleaning Data (astype, duplicates, string cleaning) to fix types, duplicates, and messy text.

Was this page helpful?

Let us know how we did