Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge 2 columns with priority

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
like image 255
Vinh Avatar asked Oct 29 '25 08:10

Vinh


1 Answers

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 == '')
like image 91
jezrael Avatar answered Oct 31 '25 05:10

jezrael