I have 2 dataframes in Pandas:
df1 = pd.DataFrame({"ID": [1, 2, 5, 9, 15, 44]})
df2 = pd.DataFrame({"col1": [a, b, c, d],
"col2":[[1, 2, 3, 4, 5], [6, 9, 15, 16, 18], [44], [88, 564, 9856]})
I am interested in matches between df1["ID"] and df2["col2"]. If there are any matches, I would like to highlight them in df2.
I found some inspiration here: comparing two columns and highlighting differences in dataframe
And here: colour element of a list by looping over another list
This is what I have and is not correct.
def highlight():
df = pd.DataFrame(columns=d.columns, index=d.index)
df2.loc[df2['col2'].eq(df1['ID']), ['col1', 'col2']] = 'background: yellow'
return df
df1.style.apply(highlight, axis=None)