I've got a dataframe which, for MVP purposes, looks like:
Val_x, Val_y
NaN, 2
1, Nan
And I'm trying to generate a new column, val
, which is the concatenation of these two.
Currently I'm doing it via:
df['Val']=[df.iloc[j]['Val_x'] if pd.isnull(df.iloc[j]['Val_y']) else df.iloc[j]['Val_y'] for j in range(len(df))]
But this is not very speed efficient, nor does it feel very pythonic/pandas-native.
I.e. I want to arrive at:
Val_x, Val_y, val
Nan, 2, 2
1, NaN, 1
Where I can then do drop(val_x),drop(val_y)
If anyone has a pointer I'd appreciate it?
Thanks
Edit:
I'm trying to generate a new column, which is effectively the 'Non-NAN' value of two other columns. The reason for this is that, for technical reasons, I've had to split my dataframe into two objects to process differently, and now wish to re-join it. So my original dataframe was index, val
, and I've now got a new dataframe which is index, val_x, val_y
, where some of val_x
is NaN
, and some of val_y
is NaN
, but never both NaN
, and now wish to simplify that down into one column again