I am trying to create a new column based on both columns. Say I want to create a new column z, and it should be the value of y when it is not missing and be the value of x when
The update
method does almost exactly this. The only caveat is that update
will do so in place so you must first create a copy:
df['z'] = df.x.copy()
df.z.update(df.y)
In the above example you start with x
and replace each value with the corresponding value from y
, as long as the new value is not NaN
.