0

I tried to check if a variable is equal to np.nan in the code(4th last line). But somehow I don't get a desired output while doing this rather when I use np.nan with check condition it sort of ignores the statement itself, the code seems to run well for other values.

My code:

import pandas as pd
import numpy as np
d={}
f=0
data=pd.read_excel('Book1.xlsx')
for i in data[:]:
    d[i]=0
    f=0
    for j in data[i]:
        print(j)
        if j==np.nan:
            f=f+1
            d[i]=f
print(d)

Please tell if I'm doing something wrong, and how to resolve this?

2 Answers2

1

Instead of

if j==np.nan:

use

if np.isnan(j):
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Nour-Allah Hussein
  • 1,439
  • 1
  • 8
  • 17
0

Change the line if j==np.nan: to if np.isnan(j):

LMc
  • 12,577
  • 3
  • 31
  • 43