Given the following dataframe df
, where df['B']=df['M1']+df['M2']
:
A M1 M2 B
1 1 2 3
1 2 NaN NaN
1 3 6 9
1 4 8 12
1 NaN 10 NaN
1 6 12 18
I want the NaN
in column B
to equal the corresponding value in M1
or M2
provided that the latter is not NaN
:
A M1 M2 B
1 1 2 3
1 2 NaN 2
1 3 6 9
1 4 8 12
1 NaN 10 10
1 6 12 18
This answer suggested to use:
df.loc[df['B'].isnull(),'B'] = df['M1']
, but the structure of this line allows to consider either M1
or M2
, and not both at the same time.
Ideas on how I should change it to consider both columns?
EDIT
Not a duplicate question! For ease of understanding, I claimed that df['B']=df['M1']+df['M2']
, but in my real case, df['B']
is not a sum and comes from a rather complicated computation. So I cannot apply a simple formula to df['B']
: all I can do is change the NaN
values to match the corresponding value in either M1
or M2
.