Issue
I have a list and I want to check if there is a missing value or not! I used two different ways to check it but didn't get the same outputs!
np.nan in lst1
output for this one is "False"
n = 0
for i in range(len(lst1)):
if lst1[i] is np.nan :
n+=1
print(n)
and for this one is 1!
the second answer is actually true but I don't know why in the first code I got "False"
Solution
To use math.isnan(x) is preferred way of NaN finding. As I understand np.nan and pure python NaN are not the same objects. And you get False at checking.
import math
import numpy as np
lst1 = [1,2,3, float('nan'), 2, np.nan, 4]
for ix, vl in enumerate(lst1):
if math.isnan(vl):
print(ix, vl)
for ix, vl in enumerate(lst1):
if vl is np.nan:
print(ix, vl)
Output for math.isnan()
3 nan
5 nan
Output for np.nan check
5 nan
Answered By - Andy Pavlov
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.