all cheat sheets

pandas missing values cheat sheet

Find, drop and fill NaN / None, plus how nulls behave in groupby and comparisons.

Finding

Count nulls per column
df.isna().sum()
Rows with any null
df[df.isna().any(axis=1)]

Fixing

Drop
df.dropna(subset=["name"])
Fill with a value or statistic
df["salary"] = df["salary"].fillna(df["salary"].median())
Fill within groups
df["salary"] = df.groupby("dept")["salary"].transform(lambda s: s.fillna(s.mean()))

Gotchas

NaN is never equal to NaN
np.nan == np.nan   # False
df["salary"].isna() # use this instead
groupby drops null keys
df.groupby("name", dropna=False).size()

Try it live

python · editable
loading editor…

Practice it

learn pandas step by step →

More cheat sheets

Ready for interview-level questions? Pro unlocks the full company problem set.