Finding common elements between multiple dataframe columns
Simplest way is to use set
intersection
list(set(df1.A) & set(df2.A) & set(df3.A))
['dog']
However if you have a long list of these things, I'd use reduce
from functools
. This same technique can be used with @cᴏʟᴅsᴘᴇᴇᴅ's use of np.intersect1d
as well.
from functools import reduce
list(reduce(set.intersection, map(set, [df1.A, df2.A, df3.A])))
['dog']
The problem with your current approach is that you need to chain multiple isin
calls. What's worse is that you'd need to keep track of which dataframe is the largest, and you call isin
on that one. Otherwise, it doesn't work.
To make things easy, you can use np.intersect1d
:
>>> np.intersect1d(df3.A, np.intersect1d(df1.A, df2.A))
array(['dog'], dtype=object)
Similar method using functools.reduce
+ intersect1d
by piRSquared:
>>> from functools import reduce # python 3 only
>>> reduce(np.intersect1d, [df1.A, df2.A, df3.A])
array(['dog'], dtype=object)