-1

This is the output of half expression mentioned in the last:

row['dep']
Out[302]: nan

However I am not getting BU in the cell 17,5.

if row['dep'] == 'nan':
     local.Cells(17,5).Value = "BU"

Can anyone please help

Prune
  • 76,765
  • 14
  • 60
  • 81
yashul
  • 103
  • 1
  • 11

3 Answers3

1

When you say 'nan' in your code, you are creating a string containing the letters 'n', 'a' and 'n'. The semantic value nan is not represented as a string in Python. You can get nan as float('nan').

Since nan is a special value and not a number (that's what NaN literally stands for, by the way), you cannot use == for comparison.

>>> float('nan') == float('nan')

will return False.

For checking if a variable is nan, it is best to use math.isnan.

Rohan Saxena
  • 3,133
  • 2
  • 16
  • 34
0

By definition, NaN fails any value comparison. The operation == NaN will return False regardless of what's on the left side. NaN == NaN is False.

Instead, use the provided operator for this very purpose:

if isnan(row['dep']):
     local.Cells(17,5).Value = "BU"
Prune
  • 76,765
  • 14
  • 60
  • 81
0

Your if statement evaluates to False hence further part is not executed. This is because nan == nan evaluates to False as nan (not a number) is not defined and you cannot compare things which are not defined. Try changing your logic or using isna(), fillna() function.

bkshi
  • 300
  • 1
  • 6
  • 22