How to Count NaN in a pandas DataFrame
TLDR solution
# grouped by column
df.isna().sum()
# total
df.isna().sum().sum()
Step-by-Step Example
Let's say, you have the following DataFrame:
df
a b
0 10.0 1.0
1 3.0 NaN
2 5.0 4.0
3 -20.0 0.0
4 NaN NaN
You can then use the isna() and sum() methods to count the NaN values:
print(f"NaNs by column: {df.isna().sum()}")
print(f"Total number of NaNs: {df.isna().sum().sum()}")
The result:
NaNs by column:
a 1
b 2
dtype: int64
Total number of NaNs: 3
That's it! You just learned how to count NaN values in a pandas DataFrame.