简体   繁体   中英

Joining two DataFrames in with Pandas if one column matches a set of other columns

I am trying to join/merge two Pandas DataFrames, but want to join rows if one column matches any of a set of columns on the second frame. Example with two columns (I have eight in my real set).

Multiple matches are not possible for my data.

df1 =

    person  fav second_fav
0   adam    xxx cherry
1   bob banana  yyy

df2 =

    fruit   fruit_val
0   apple   10
1   banana  20
2   cherry  30

desired output:

    person  fav     second_fav    fruit_val
0   adam    xxx     cherry        30
1   bob     banana  yyy           20

Merge two dataframes twice using each criteria can help to achieve this

import pandas as pd
df1 = pd.DataFrame(data = {'person':['adam','bob'],
                           'fav':['xxx','banana'],
                           "second_fav":['cherry','yyy'],})

df2 = pd.DataFrame(data = {'fruit':['apple','banana','cherry'],
                           'fruit_val':[10,20,30],})

First inner join using first criteria

result1 = pd.merge(df1, df2, left_on=['fav'], right_on=['fruit'], how='inner', sort=True)

Second join

result2 = pd.merge(df1, df2, left_on=['second_fav'], right_on=['fruit'], how='inner', sort=True)

Append the two dataframes

result_final = result1.append(result2)

result_final
Out[14]: 
  person     fav second_fav   fruit  fruit_val
0    bob  banana        yyy  banana         20
0   adam     xxx     cherry  cherry         30

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM