You can check if any value is NaN in a dataframe in Pandas in Python by using the following 2 methods.
Method 1: check if any value is NaN by columns:
df.isnull().any()
Method 2: Check if any value is NaN in the whole dataframe:
df.isnull().any().any()
Example for Method 1
The following checks if any value is NaN in the dataframe by columns using df.isnull().any().
import pandas as pd
import numpy as np
# Create a dataframe with NaN
df = pd.DataFrame({'Col_1': [100, np.nan, 200, np.nan, 500],
'Col_2': [np.nan, 30, 100, 88, 55],
'Col_3': [88, 87, 79, 88, 55]})
# print out the dataframe with NaN
print('Dataframe with NaN: \n', df)
# check if any value is NaN in dataframe by columns
df.isnull().any()
The following is the output showing there is NaN in the first two columns.
Dataframe with NaN:
Col_1 Col_2 Col_3
0 100.0 NaN 88
1 NaN 30.0 87
2 200.0 100.0 79
3 NaN 88.0 88
4 500.0 55.0 55
Col_1 True
Col_2 True
Col_3 False
dtype: bool
Example for Method 2
The following checks if any value is NaN in the whole dataframe using df.isnull().any().any().
import pandas as pd
import numpy as np
# Create a dataframe with NaN
df = pd.DataFrame({'Col_1': [100, np.nan, 200, np.nan, 500],
'Col_2': [np.nan, 30, 100, 88, 55],
'Col_3': [88, 87, 79, 88, 55]})
# print out the dataframe with NaN
print('Dataframe with NaN: \n', df)
# check if any value is NaN in the whole dataframe
df.isnull().any().any()
The following is the output showing there is at least one NaN in the whole dataframe.
Dataframe with NaN:
Col_1 Col_2 Col_3
0 100.0 NaN 88
1 NaN 30.0 87
2 200.0 100.0 79
3 NaN 88.0 88
4 500.0 55.0 55
True
Further Reading
The following are other tutorials related to NaN.