Pandas: Updating dataframe values for rows where one colum has missing data

219 Views Asked by At

I have a dataframe called firstpart.

I'm trying to update the values in one column (Key), but only for rows in which another column (Zone) has no data.

I'm using this code, which doesn't work:

firstpart.ix[firstpart.Zone ==np.nan,"Key"] = "newvalue"

Neither does this:

firstpart.ix[firstpart.Zone =="","Key"] = "newvalue"

Using this syntax I'm able to update values in rows for which Zone has another value, but for some reason not if I try to select the rows in which it is blank.

What am I doing wrong?

1

There are 1 best solutions below

0
dmvianna On BEST ANSWER
firstpart.ix[firstpart.Zone.isnull()] = "newvalue"

You can't equate NaN to anything.

In [1]: NaN == NaN
Out[1]: False

You need special methods for that, and this is what .isnull() is about.