Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to join pandas dataframe on 2 columns?

Tags:

python

pandas

Assume the following DataFrames

df1:

id    data1
1     10
2     200
3     3000
4     40000

df2:

id1    id2    data2
1      2      210
1      3      3010
1      4      40010
2      3      3200
2      4      40200
3      4      43000

I want the new df3:

id1    id2    data2    data11    data12        
1      2      210      10        200
1      3      3010     10        3000
1      4      40010    10        40000 
2      3      3200     200       3000
2      4      40200    200       40000
3      4      43000    3000      40000

What is the correct way to achieve this in pandas?


Edit: Please not the specific data can be arbitrary. I chose this specific data just to show where everything comes from, but every data element has no correlation to any other data element.


Other dataframes examples, because the first one wasn't clear enough:

df4:

id    data1
1     a
2     b
3     c
4     d

df5:

id1    id2    data2
1      2      e
1      3      f
1      4      g
2      3      h
2      4      i
3      4      j

I want the new df6:

id1    id2    data2    data11    data12        
1      2      e        a         b  
1      3      f        a         c
1      4      g        a         d
2      3      h        b         c
2      4      i        b         d
3      4      j        c         d

Edit2: Data11 and Data12 are simply a copy of data1, with the corresponding id id1 or id2

like image 618
Gulzar Avatar asked Dec 22 '22 22:12

Gulzar


1 Answers

1.First merge both dataframe using id1 and id column
2.rename data1 as data11
3. drop id column
4. Now merge df1 and df3 on id2 and id

df3 = pd.merge(df2,df1,left_on=['id1'],right_on=['id'],how='left')
df3.rename(columns={'data1':'data11'},inplace=True)
df3.drop('id',axis=1,inplace=True)

df3 = pd.merge(d3,df1,left_on=['id2'],right_on=['id'],how='left')
df3.rename(columns={'data1':'data12'},inplace=True)
df3.drop('id',axis=1,inplace=True)

I hope it would solve your problem

like image 69
tawab_shakeel Avatar answered Dec 25 '22 11:12

tawab_shakeel