-6

I have two dataframes, df and df1. The first one contains all the information about all the possible combination of a dataset while the second one is just a subset without the information.

df

x   y   distance    
0   1      4
0   2      3
0   3      2
1   2      2
1   3      5
2   3      1

df1

x   y       
1   3      
2   3      
2   3      

I would like to merge df and df1 in order to have the following:

df1

x   y   distance    
1   3      5
2   3      1
2   3      1
1
  • @RexLow Although there is a slight difference that here it's not necessary the indexes do match but the columns x,y. Commented Mar 13, 2018 at 10:50

1 Answer 1

0

You can use the merge command

df.merge(df1, left_on=['x','y'], right_on=['x','y'], how='right')

Here you're merging the df on the left with df1 on the right using the columns x andy as merging criteria and keeping only the rows that are present in the right dataframe.

You can read more about merging and joining dataframes here.

Sign up to request clarification or add additional context in comments.

2 Comments

Not OP but getting through the same kind of problems. Would you care to explain why you need to include right_on=['x','y'] please? Thanks.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.