Handling NaN (isnull/dropna/fillna)
NaN marks missing data; you can find it, remove it or fill it in.
In this page:
Syntax
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)
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
- Filling missing values without thinking about the meaning
- Dropping too many rows with dropna
- 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: