Check If Any Value is NaN in pandas DataFrame in Python (Example)

 

In this Python tutorial you’ll learn how to test for NaN values in a pandas DataFrame.

The content of the tutorial is structured as follows:

If you want to learn more about these contents, keep reading!

 

Exemplifying Data & Add-On Libraries

If we want to use the functions and commands of the pandas library, we first need to import pandas:

import pandas as pd                                     # Import pandas

We use the following data as basement for this Python programming tutorial:

data = pd.DataFrame({'x1':[1, 1, float('NaN'), 1, 1],  # Create example DataFrame
                     'x2':range(1, 6),
                     'x3':['a', 'b', 'c', 'd', 'e']})
print(data)                                            # Print example DataFrame

 

table 1 DataFrame check if any value is nan pandas dataframe python

 

Table 1 shows that our example data is composed of five rows and the three variables “x1”, “x2”, and “x3”.

As you may already have seen in Table 1, our pandas DataFrame contains one NaN value (i.e. a missing value; Not a Number) in the first column.

However, let’s check that by using some Python code!

 

Example: Test Whether pandas DataFrame Contains NaN Values Using isnull() & any() Functions

This example illustrates how to check if any data cell in a pandas DataFrame is NaN.

For this task, we can apply the isnull and any functions in combination with the values attribute as you can see below:

print(data.isnull().values.any())                      # Apply isnull & any
# True

After running the previous Python syntax, the logical value True is returned. In other words: Our example data contains at least one NaN value.

 

Video & Further Resources on this Topic

In case you need further info on the examples of this tutorial, I recommend having a look at the following video on my YouTube channel. In the video instruction, I’m explaining the Python programming syntax of this article in more detail:

 

 

Do you still need more info on this topic? Then you may watch the following video on the Data School YouTube channel. In the video, the speaker demonstrates how to handle missing values in Python.

 

 

Besides that, you might want to read the related tutorials on this homepage:

 

You have learned in this article how to check for NaN values in a pandas DataFrame in Python. In case you have additional questions, don’t hesitate to let me know in the comments section.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top