简体   繁体   中英

How to compare a dataframe to a subset intersection of another dataframe in pandas in python?

I have a the following dataframes in python:

dataframe 1

             1  2  3  4  5
dog   dog    0  0  0  0  0
      fox    0  0  0  0  0
      jumps  0  0  0  0  0
      over   0  0  0  0  0
      the    0  0  0  0  0
fox   dog    0  0  0  0  0
      fox    0  0  0  0  0
      jumps  0  0  0  0  0
      over   0  0  0  0  0
      the    0  0  0  0  0
jumps dog    0  0  0  0  0
      fox    0  0  0  0  0
      jumps  0  0  0  0  0
      over   0  0  0  0  0
      the    0  0  0  0  0
over  dog    0  0  0  0  0
      fox    0  0  0  0  0
      jumps  0  0  0  0  0
      over   0  0  0  0  0
      the    0  0  0  0  0
the   dog    0  0  0  0  0
      fox    0  0  0  0  0
      jumps  0  0  0  0  0
      over   0  0  0  0  0
      the    0  0  0  0  0

dataframe 2

             1  2  4  5
dog   dog    0  0  0  0
      fox    0  0  0  0
      jumps  0  0  0  0
      the    0  0  0  0
      horse  0  0  0  0
fox   dog    0  0  0  0
      fox    0  0  0  0
      over   0  0  0  0
      the    0  0  0  0
      cat    0  0  0  0

You can see that dataframe2 contains multiindexes of dataframe1 but it also contains additional multiindexes like horse and cat. Dataframe 2 also doesn't contain all the columns of dataframe 1 as you can see it misses column 3.

I want to compare these two dataframes in such a way that the function returns True if the values of any common first->second (multi) indices between the two dataframes also match each other, otherwise False.

I could iterate over by looking over each value manually but that would increase the function's time complexity. Does any know if pandas provides a builtin way of doing this or do I need to construct a function myself. If so, can you point me in the right direction? Any suggestions are highly appreciated. Thank you.

IIUC, you can use eq :

df1.eq(df2)

Output:

                 1      2      3      4      5
dog   dog     True   True  False   True   True
      fox     True   True  False   True   True
      horse  False  False  False  False  False
      jumps   True   True  False   True   True
      over   False  False  False  False  False
      the     True   True  False   True   True
fox   cat    False  False  False  False  False
      dog     True   True  False   True   True
      fox     True   True  False   True   True
      jumps   True   True  False   True   True
      over   False  False  False  False  False
      the     True   True  False   True   True
jumps dog    False  False  False  False  False
      fox    False  False  False  False  False
      jumps  False  False  False  False  False
      over   False  False  False  False  False
      the    False  False  False  False  False
over  dog    False  False  False  False  False
      fox    False  False  False  False  False
      jumps  False  False  False  False  False
      over   False  False  False  False  False
      the    False  False  False  False  False
the   dog    False  False  False  False  False
      fox    False  False  False  False  False
      jumps  False  False  False  False  False
      over   False  False  False  False  False
      the    False  False  False  False  False

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM