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
Let's say DataFrame is called df. First copy the y column.
df
y
df["z"] = df["y"].copy()
Then set the nan locations of z to the locations in x where the nans are in z.
import numpy as np df.z[np.isnan(df.z)]=df.x[np.isnan(df.z)] >>> df x y z 0 1 NaN 1 1 2 8 8 2 4 10 10 3 8 NaN 8