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
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
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.
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"
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.