How to Check for NaN in a pandas DataFrame
TLDR solution
# check if there is a nan in the whole df
df.isna().values.any()
# check a specific column
df['column'].isna().values.any()
# show the row where a specific column has a nan
df.loc[df['column'].isna()]
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
Check if There are any NaN Values
Use the following code to check if there is at least one NaN value in your DataFrame:
df.isna().values.any()
The output:
True
Check if a Specific Column has a NaN Value
df['a'].isna().values.any()
Show all Rows That Have a NaN in a Specific Column
df.loc[df['a'].isna()]
Count NaN Values
df.isna().sum() # grouped by column
df.isna().sum().sum() # total
That's it! You just learned how to check for NaN values in a pandas DataFrame.