multigoodverse
multigoodverse

Reputation: 8072

Apply conditional on two pandas dataframe columns

I have this dataframe:

enter image description here

I want to create a ZIP column which will get the value of ZIP_y when ZIP_x is NaN and the value of ZIP_x when ZIP_x is not NaN.

I tried this code:

dm["ZIP"]=numpy.where(dm["ZIP_x"] is numpy.nan, dm["ZIP_y"],dm["ZIP_x"])

But that gave me this output:

enter image description here

As you can see, the ZIP column seems to be getting the values of ZIP_x in each of its cells.

Do you know how to achieve what I am after?

Upvotes: 1

Views: 134

Answers (1)

EdChum
EdChum

Reputation: 394041

You want this:

dm["ZIP"]=numpy.where(dm["ZIP_x"].isnull(), dm["ZIP_y"],dm["ZIP_x"])

You can't use is or == for that matter to compare NaNs

Upvotes: 1

Related Questions