保存前着色数据框

Coloring dataframe before saving

我创建了一个 df,其中的值为 *"Yes"、"No" "Maybe"。*

我想在保存到 excel 之前让这个单元格变得丰富多彩。如果我使用库 xlwt,我有这样的东西:

sheet2.write(row, col, str('Allowed'), style)

但是,我不想再次打开 excel,它已经用 pandas 创建,以便通过并用我的颜色填充它。我想用 excel.

格式的所需颜色保存我的数据框

我的数据框是这样的:

我想要的输出是:

关于如何做到这一点有什么建议吗?

是这样的吗?

def color_df(val): 
    if val == 'Yes':
        color = 'red'
    elif val == 'Maybe':
        color = 'Orange'
    else:
        color = 'Blue'
    return 'color: %s' % color

df = df.style.applymap(color_df).to_excel(path_var + 'styled.xlsx', engine='xlsxwriter')