+1 vote
in Programming Languages by (9.3k points)

In my data, some values are NaN and my code fails because of those values. How can I check NaN values in the data?

1 Answer

0 votes
by (18.7k points)

There are functions in NumPy, math, and pandas libraries that you can use to check NaN values. Here is a simple example to check if a value is NaN.

>>> import math
>>> import numpy as np
>>> import pandas as pd

>>> t=float('nan')
>>> t
nan
>>> math.isnan(t)
True
>>> pd.isna(t)
True
>>> np.isnan(t)
True

>>> b=0.1
>>> np.isnan(b)
False
>>> math.isnan(b)
False
>>> pd.isna(b)
False

...