'Pandas style.apply matches between 2 dataframes with .isin

I have two dataframes and I want to check for their matches.

Dataframe1

some_stuff    big_number
a             111
b             112
c             117

Dataframe2

another_big_num
0111
0114
0117

If the number of df2 exists in df1, I want to highlight the entire row. I understand that by using .isin I can check for the matches but I don't know how I can use .style.applymap provided by pandas in such a context.

So doing something like df_test1["big_number"].isin(df_test2["another_big_num"]) gives me the matches I want but I don't know where to go from there.



Solution 1:[1]

You can do something like:

def big_number(s,x):
    if s.big_number in x:
        return ['background-color: yellow'] * len(s)
    else:
        return ['background-color: white'] * len(s)

df_test1.style.apply(big_number(df_test1,df_test2["another_big_num"].values), axis=1)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 LazyCoder