0

我正在尝试更改 Pandas 中 df3 中 df1 中字符串的字体颜色。我的数据集是:

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'])

要更改 df1 中字符串的颜色,我正在使用以下内容,但出现无效的语法错误。请帮忙。

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
4

1 回答 1

0

使用单词边界检查子串contains并返回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)

图片

注意 - 如果只想选择子字符串,尚不支持。

于 2018-05-02T19:15:39.597 回答