0
for r in range(65000):
    for c in range(8):
        if df1.iloc[r,c] != NaN:
            k=k+1
            df.iloc[k,3] = df1.iloc[r,c]
        else:
            print("Nan Detected")
            l=l+1
print(l," Nan Values encountered")
DeepSpace
  • 78,697
  • 11
  • 109
  • 154
Czar.Wolfgang
  • 67
  • 2
  • 3
  • 12
  • 3
    Possible duplicate of [How to check for NaN in python?](https://stackoverflow.com/questions/944700/how-to-check-for-nan-in-python) – YiFei Jul 12 '17 at 16:06
  • I need to use this comparison in an If statement. How would you use math.isnan in an if statement for comparison with a value... for example: for i in range(len(df)): for j in range(8): if math.isnan(df1.iloc[i,j]) == FALSE: b=b+1 print(b ," correct out of", b+c) else: print("Error") I get error at line where If statement begins. Type Error: a float is required. – Czar.Wolfgang Jul 12 '17 at 19:10
  • 1
    Then what is the type for `df1.iloc[i,j]`? Try insert `print(type(df1.iloc[r, c]))` between last for and the if. – YiFei Jul 13 '17 at 01:07
  • It's numpy, float64 – Czar.Wolfgang Jul 17 '17 at 14:11
  • 1
    Try `numpy.isnan(x)` then, I don't use numpy, but that is covered in the link I previously gave. – YiFei Jul 18 '17 at 01:57

1 Answers1

13

Unfortunately NaN will compare false, even with itself. So df1.iloc[r,c] != NaN is always true.

Use numpy.isnan(number) or math.isnan(number) instead to check if number is NaN.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • for i in range(len(df)): for j in range(8): if math.isnan(df1.iloc[i,j]) == FALSE: b=b+1 print(b ," correct out of", b+c) else: print("Error") The Error I get is at if statement. Type Error : a float is required. – Czar.Wolfgang Jul 12 '17 at 18:47