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
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

Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top