简体   繁体   中英

merge two dataframes using two keys with or operator

I have two dataframes on financial data. Each has two keys, ticker and cusip. I wanted to merge these two datafames based on matching of any of those two keys (not necessary their interaction). If I used

`pd.merge(a , b, how='left' ,  on=['ticker', 'cusip'])

it would return just those observations with matched ticker and cusip. I need to have or operator. something like this in sql is qhat I am asking:

select * from a left join b on a.ticker = b.ticker or a.cusip=b.cusip

I would appreciate your hints.

Let's try this:

df1 = pd.merge(a, b, how='left', on='ticker')
df2 = pd.merge(a, b, how='left', on='cusip')

df_out = pd.concat([df1, df2])

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