0

I have 2 Pandas dataframe and I want to find the length of the intersection of 2 columns in order. For example:

    df1 = 

 a           b
 a1          b1
 a2          b2
 a3          b3

  df2 =
 a           b
 a1          b1
 a4          b4
 a2          b4



 Answer: 1  only 1 pair is in common (a1.  b1)

For 1 column, I can simply write:

 set(df1.a).intersection(set(df2.a))

How to do this for 2 columns without merging the 2 data frames and is it possible?

EDIT: I want it without merging

learner
  • 857
  • 1
  • 14
  • 28

1 Answers1

2

You can use merge:

df1.merge(df2, on=['a','b'])

Output:

    a   b
0  a1  b1
Quang Hoang
  • 146,074
  • 10
  • 56
  • 74