1

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.

FaCoffee
  • 7,609
  • 28
  • 99
  • 174

1 Answers1

7

Base on our discussion above in the comment

df.B=df.B.fillna(df[['M1','M2']].max(1))
df
Out[52]: 
   A   M1    M2     B
0  1  1.0   2.0   3.0
1  1  2.0   NaN   2.0
2  1  3.0   6.0   9.0
3  1  4.0   8.0  12.0
4  1  NaN  10.0  10.0
5  1  6.0  12.0  18.0

From jezrael

df['B']= (df['M1']+ df['M2']).fillna(df[['M2','M1']].sum(1))
BENY
  • 317,841
  • 20
  • 164
  • 234