Python 巨蟒熊猫风格

Python 巨蟒熊猫风格,python,pandas,Python,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

我正在尝试更改熊猫中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

使用单词边界检查子字符串,并返回样式为s的数据帧:

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)

注意-如果只想选择子字符串,目前还不支持