1

I'm looking for a way to select the rows from Dataframe 1 where the values of column 'A' and 'B' appear the same as in Dataframe 'a' and 'b'. The corresponding column names are different, so 'A' is not the same as 'a'.

Example:

Dataframe 1:

    A   B   C
0  10  20  30
1  40  50  60
2  70  80  90

Dataframe 2:

    a   b
0  10  20
1  40   0
2  70  80

The selected rows would be:

    A   B   C
0  10  20  30
2  70  80  90
  • are you looking for `A` to `a` and `B` to `b` matching or element wise match based on the second dataframe, what if the second dataframe had columns `c` and `d` ? – anky Sep 27 '19 at 12:57

1 Answers1

3

You need a merge and index on the columns of df1:

df1.merge(df2, left_on=['A', 'B'], right_on=['a','b'])[df1.columns]

   A   B   C
0  10  20  30
1  70  80  90
yatu
  • 86,083
  • 12
  • 84
  • 139
  • Thanks! Can I somehow get the rows that are excluded? – Kristóf Kövér Sep 27 '19 at 13:21
  • Yes, just exclude the indexing step `df1.merge(df2, left_on=['A', 'B'], right_on=['a','b'])` @KristófKövér Oh and don't forget you can upvote and accept – yatu Sep 27 '19 at 13:22
  • I mean getting the excluded rows, not the columns come with df2. What I mean is practically splitting df1 into two dataframes by the condition this merge defines. – Kristóf Kövér Sep 27 '19 at 13:25
  • Yes, can't cose now but check the indicator argument @kristof – yatu Sep 27 '19 at 13:27