-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
emax
  • 6,965
  • 19
  • 74
  • 141

1 Answers1

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.

Ignacio Vergara Kausel
  • 5,521
  • 4
  • 31
  • 41
  • 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. – Bowen Liu Sep 07 '18 at 18:52
  • @BowenLiu Maybe this might help: https://stackoverflow.com/questions/53645882/pandas-merging-101 – cs95 Feb 07 '19 at 10:58