I have a pandas DataFrame, something like:
col1 col2 col3 col5
NaN 1 2 8
2 NaN 4 8
4 NaN 4 8
I want to do two t
To get the new column, you could use fillna
(or combine_first
):
df['newcol1'] = df.col1.fillna(df.col2)
Then for the subtraction, use sub
and specify axis=0
since we want to consider the row indices when matching up labels (not the column indices as is the default):
>>> df[['newcol1', 'col3']].sub(df['col5'], axis=0)
newcol1 col3
0 -7 -6
1 -6 -4
2 -4 -4