yssefunc
yssefunc

Reputation: 91

How can I find intersect dataframes in pandas?

I have two dataframes like this.But, column zero is a ndarray. I want to find intersect dataframes.

a1 =

 0                   |  1
 [39]                |  6000000
 [49] [50] [51] [52] |  84100 
 [49]                |  95400
 [20]                |  65089

a2 =

 0                   |  1
 [49] [50] [51] [52] |  84100 
 [38] [50]           |  530400
 [52]                |  60611
 [20]                |  65089

expected output:

a3 =

 0                   |  1
 [49] [50] [51] [52] |  84100 
 [20]                |  65089

Any ideas would be appreciated.

Upvotes: 0

Views: 231

Answers (1)

FChm
FChm

Reputation: 2600

You should be able to just make a boolean mask by comparison using the numpy.array.all method:

a1 = pd.DataFrame({'a':[[0], [0,1,2], [3], [4]], 
                    'b':[0, 1000, 2000, 3000]})
a2 = pd.DataFrame({'a':[[0], [0,1,2], [4], [6]], 
                    'b':[0, 1000, 88000, 6000]})

a3 = a1[(a1==a2).values.all(axis=1)]

which returns:

     a            b
0      [0]        0
1   [0, 1, 2]   1000

Upvotes: 1

Related Questions