alexa
Facebook
Twitter
LinkedIn
Instagram
Whatsapp
Call Now
Quick Inquiry

Merging Pandas dataframe on 2 columns in either order without duplication in python ?

Merging Pandas dataframe on 2 columns in either order without duplication in python ?

Try this:

 df1['key'] = df1[l].where(df1[l].isin(df2[l].stack().tolist())).fillna(0).apply(frozenset,axis=1)
df2['key'] = df2[l].fillna(0).apply(frozenset,axis=1)
ndf = pd.merge(df1,df2[['key','Value']],on = 'key',how='left').drop('key',axis=1) 

Output:

  id_A  id_B value  Value
0  0001  0010     A      2
1  0002  0020     B      1
2  0003  0030     C      4
3  0004  0040     D      3
4  0005  0050     E      5

33 0
7

Write a Comments


* Be the first to Make Comment

GoodFirms Badge
GoodFirms Badge

Fix Your Meeting With Our SEO Consultants in India To Grow Your Business Online