Categories
Python Answers

How to check for NaN values with Python?

Spread the love

Sometimes, we want to check for NaN values with Python.

In this article, we’ll look at how to check for NaN values with Python.

How to check for NaN values with Python?

To check for NaN values with Python, we can use the math.isnan method.

For instance, we write:

import math

x = float('nan')
isnan = math.isnan(x)
print(isnan)

We call float with a non-number string and assign that to x.

Then we call math.isnan with x to check if x is NaN or not and assign the returned result to isnan.

Therefore, isnan is True since x is not a number.

Conclusion

To check for NaN values with Python, we can use the math.isnan method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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