← Back to Pandas Course | Chapter 5: Data Cleaning | Lesson 1 of 7

Handling NaN (isnull/dropna/fillna)

NaN marks missing data; you can find it, remove it or fill it in.
Syntax
python
df.isnull().sum()
df.dropna()
df.fillna(value)

Handling NaN (isnull/dropna/fillna)

isnull() (alias isna) returns True where values are missing, and sum() on that counts them per column. dropna() removes rows or columns with missing values, and fillna(value) replaces them.

You can fill with a constant, the mean, or the previous value using ffill.

Note: df.isnull().sum() is the classic first check for missing data.

Example: Handling NaN (isnull/dropna/fillna)

python
import pandas as pd
import numpy as np

df = pd.DataFrame({"a": [1, np.nan, 3], "b": ["x", "y", None]})
print(df.isnull().sum())
print(df.dropna())
print(df.fillna({"a": df["a"].mean(), "b": "unknown"}))

# Output:
# a    1
# b    1
# dtype: int64
#      a  b
# 0  1.0  x
#      a        b
# 0  1.0        x
# 1  2.0        y
# 2  3.0  unknown
Related Topics
Common Mistakes
  1. Filling missing values without thinking about the meaning
  2. Dropping too many rows with dropna
  3. Testing NaN with ==
Chapter Summary
  • isnull finds missing values
  • dropna removes them
  • fillna replaces them
  • isnull().sum() counts per column
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.