0
votes

I have two dataframes, df1 and df2:

df1 = pandas.DataFrame({'w':['a','b','c'], 'x':['d','e','f']})


df2 = pandas.DataFrame({'w':['b','a','g','c'], 'x':['h','d','i','f'],'y':['j','k','l','m'],'z':['n','o','p','q']})

In [106]: df1
Out[106]: 
   w  x
0  a  d
1  b  e
2  c  f

In[107]: df2
Out[107]: 
   w  x  y  z
0  b  h  j  n
1  a  d  k  o
2  g  i  l  p
3  c  f  m  q

Both dataframes have same columns df1 and df2. I need to add columns 'y' and 'z' on df2 to df1 such that the values in columns 'w' and 'x' are identical.

The end result would be:

  w  x  y  z
0 a  d  k  o
1 c  f  m  q
1
df2 does not have b and e in w and x respectively. Can you explain their values in your end result? - harvpan
Sorry, I have edited my original post. - DPdl
df1.merge(df2) gets your desired output if I understand correctly - sacuL
@sacul, that is what my answer says. Still downvoted. :( - harvpan
To be fair, you were downvoted when your result didn't match the desired result, then the question was edited. - user3483203

1 Answers

1
votes

You can use .merge() .

df1.merge(df2)

Output :

    w   x   y   z
0   a   d   k   o
1   c   f   m   q