0

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)
BeRT2me
  • 12,699
  • 2
  • 13
  • 31
Mr.Slow
  • 490
  • 1
  • 1
  • 16
  • what the expected output should look like? – Naveed Oct 01 '22 at 21:39
  • Hi, the output will be the modified df2, just with the elements matches colourfully distinguish in the lists somehow. It could be a different color of the text or a background around the element. Thank you. – Mr.Slow Oct 01 '22 at 21:55
  • Add additional information to the question with [edit]. Don't add information as a comment. – Trenton McKinney Oct 01 '22 at 22:06
  • 1
    As per the suggested duplicate, use `common = df1.ID.tolist()` and then `df.style.format(myFmt)` should work with the function mentioned there. Need to use `import re`, of course, though that isn't explicitly stated. – ouroboros1 Oct 01 '22 at 22:34
  • For some reason unknown, (just) number 9 displays always in red in any column of newly created dataframe. Do you have any idea why so? – Mr.Slow Nov 03 '22 at 08:51

0 Answers0