How to get the intersection of two data?

I have two data frames of the same format:

df1 = DataFrame({'a':[0,1,2,3,4], 'b':['q','r','s','t','u']}) df1 ab 0 0 q 1 1 r 2 2 s 3 3 t 4 4 u df2 = DataFrame({'a':[4,3,2,1,999], 'b':['u','r','s','t','u']}) df2 ab 0 4 u 1 3 r 2 2 s 3 1 t 4 999 u 

I would like to get a new data framework that has rows that appear in both of them (ignoring the index). So the above example gives a data frame

  ab 0 4 u 1 2 s 

How to get this intersection?

+5
source share
1 answer

You can just do merge , this will use all columns, and the default merge type is inner , so the values ​​should be present in both dfs:

 In [71]: df1.merge(df2) Out[71]: ab 0 2 s 1 4 u 
+9
source

Source: https://habr.com/ru/post/1216231/


All Articles