Test for NaN Values in pandas DataFrame in Python (Example Code)
In this article, I’ll illustrate how to check for NaN values in a pandas DataFrame in the Python programming language.
Preparing the Example
import pandas as pd # Import pandas library |
import pandas as pd # Import pandas library
my_df = pd.DataFrame({'A':range(10, 5, - 1), # Construct example DataFrame 'B':[float('NaN'), 0, 1, 2, 3]}) print(my_df) # Display example DataFrame in console # A B # 0 10 NaN # 1 9 0.0 # 2 8 1.0 # 3 7 2.0 # 4 6 3.0 |
my_df = pd.DataFrame({'A':range(10, 5, - 1), # Construct example DataFrame 'B':[float('NaN'), 0, 1, 2, 3]}) print(my_df) # Display example DataFrame in console # A B # 0 10 NaN # 1 9 0.0 # 2 8 1.0 # 3 7 2.0 # 4 6 3.0
Example: Checking for NaN Values in pandas DataFrame Using isnull() & any() Functions
print(my_df.isnull().values.any()) # Test for NaN # True |
print(my_df.isnull().values.any()) # Test for NaN # True