2

I have two pandas dataframe with different size. two dataframe looks like

df1 = 

x   y   data
1   2   5
2   2   7
5   3   9
3   5   2

and another dataframe looks like:

df2 =

x   y   value
5   3   7
1   2   4
3   5   2
7   1   4
4   6   5
2   2   1
7   5   8

I am trying to merge these two dataframe so that the final dataframe expected to have same combination of x and y with respective value. I am expecting final dataframe in this format:

x   y   data    value
1   2   5      4
2   2   7      1
5   3   9      7
3   5   2      2

I tride this code but not getting expected results.

dfB.set_index('x').loc[dfA.x].reset_index()

2 Answers 2

4

Use merge, by default how='inner' so it can be omit and if join only on same columns parameter on can be omit too:

print (pd.merge(df1,df2))
   x  y  data  value
0  1  2     5      4
1  2  2     7      1
2  5  3     9      7
3  3  5     2      2

If in real data are multiple same column names use:

print (pd.merge(df1,df2, on=['x','y']))

   x  y  data  value
0  1  2     5      4
1  2  2     7      1
2  5  3     9      7
3  3  5     2      2
Sign up to request clarification or add additional context in comments.

Comments

1
df1.merge(df2,by='x')

This will do

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.