Handling Missing Values (NaN)
Detect, count, drop, and fill missing data — a core data cleaning skill
Free tier: read the explanation here. Upgrade to Pro for Drills, Speed challenges & Mastery badges.
UpgradeKnowledge Debt detected
You can study this freely — but your score may plateau if these foundations have gaps. The Mastery badge requires them to be solid.
Explanation
Missing values in pandas are represented as NaN (Not a Number) or None.
Detecting:
python df.isna() # boolean DataFrame — True where NaN df.isna().sum() # count of NaN per column df.isna().any() # True for any column that has NaN
Dropping:
python df.dropna() # drop rows with ANY NaN df.dropna(axis=1) # drop COLUMNS with any NaN df.dropna(subset=['age']) # drop rows where 'age' is NaN df.dropna(thresh=3) # keep rows with at least 3 non-NaN values
Filling:
python df.fillna(0) # fill all NaN with 0 df['age'].fillna(df['age'].mean()) # fill with column mean df.fillna(method='ffill') # forward fill (propagate last valid value) df.fillna(method='bfill') # backward fill
Best practice: Always check df.isna().sum() right after loading data. Decide per column whether to drop, fill with mean/median/mode, or leave as-is.
Examples
Checking and filling missing data
Different strategies for different columns
import pandas as pd
import numpy as np
df = pd.DataFrame({'age':[25,np.nan,35,np.nan],'salary':[50000,65000,np.nan,80000]})
print(df.isna().sum())
# age 2
# salary 1
# Fill age with median, drop rows where salary is missing
df['age'] = df['age'].fillna(df['age'].median())
df = df.dropna(subset=['salary'])
print(df)How well did you understand this?
Next in pandas
groupby