Alexis Eggermont
Alexis Eggermont

Reputation: 8145

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

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?

Upvotes: 0

Views: 218

Answers (1)

dmvianna
dmvianna

Reputation: 15718

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.

Upvotes: 1

Related Questions