3

I have a pandas df with 2 columns A and B. What I need is a new merged column 'result', in which A is preferred to B. I really thought it would be easy but still no solution. How would you guys do that? Thanks for your help.

A   B   result
go  for go
go      go
go      go
    for for
    for for
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Vinh
  • 324
  • 3
  • 12

1 Answers1

4

Use combine_first or fillna:

df['result'] = df['A'].combine_first(df['B'])
print (df)
     A    B result
0   go  for     go
1   go  NaN     go
2   go  NaN     go
3  NaN  for    for
4  NaN  for    for

Or:

df['result'] = df['A'].fillna(df['B'])
print (df)
     A    B result
0   go  for     go
1   go  NaN     go
2   go  NaN     go
3  NaN  for    for
4  NaN  for    for

EDIT:

For replace empty space to NaNs use:

df = df.replace('', np.nan)

Or:

df = df.mask(df == '')
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • jezrael thank you for your reply. I actually have empty values instead of NaN in the table and couldn't reproduce your solution at first, until I understand that I have to convert all the empty values to NaN to make combine_first work. – Vinh Oct 18 '17 at 15:07
  • And I just gather enough points to be able to vote answers now, which is soooo cooool! – Vinh Oct 18 '17 at 15:10
  • You need df = df.replace('', np.nan) or df = df.mask(''). I am on phone only, so untested. – jezrael Oct 18 '17 at 15:58
  • thats exactly what I did. Thanks jezrael! – Vinh Oct 18 '17 at 16:10