0

I'm trying to change the font color of the strings in df1 that are in df3 in Pandas. My data sets are:

df1 = [ "i like to shop at store a." , "he likes to shop at the store b.", "she is happy to shop at store c.", 'we want to shop at the store d.']
df2 = [ "store a", "store b", "store c", 'store d' ]
df3 = [ "like to", "likes to shop", "at store" ]

myDataSet = list(zip(df1,df2))
df = pd.DataFrame(data = myDataSet, columns=['df1', 'df2'])

To change the color of the strings in df1, I'm using the following but get invalid syntax error. Please help.

def color_negative_red(df1):
    x for x in df3 if x in df["df1"]
    return 'color: %s' % color
s = df.style.applymap(color_negative_red)
s
Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
Steve DEU
  • 167
  • 1
  • 13

1 Answers1

0

Use word boundary for check subtrings with contains and return DataFrame of styles:

def color_substrings(x):
    c1 = 'background-color: red'
    c2 = '' 
    pat = '|'.join([r'\b{}\b'.format(x) for x in df3])
    mask = df["df1"].str.contains(pat)
    df1 =  pd.DataFrame(c2, index=df.index, columns=df.columns)
    #modify values of df1 column by boolean mask
    df1.loc[mask, 'df1'] = c1
    return df1

df.style.apply(color_substrings, axis=None)

pic

Notice - If want select substrings only, it is not supported yet.

jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252