1

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?

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.