我的数据框“df”如下:
x = [1,3,5,7]
y1 = [3,2,2,2]
y2 = [2,5,2,2]
y3 = [7,2,2,1]
df = pd.DataFrame({'x': x, 'y1': y1, 'y2': y2, 'y3': y3})
writer = pd.ExcelWriter('output.xlsx')
df.to_excel(writer,'Sheet1')
writer.save()
答案 0 :(得分:1)
如果dictionary
指定了颜色,则可以使用styles:
def color(a):
d = {1:'yellow', 3:'green', 5:'blue', 7:'red'}
d1 = {k: 'background-color:' + v for k, v in d.items()}
df1 = pd.DataFrame(index=a.index, columns=a.columns)
df1 = a.applymap(d1.get).fillna('')
return df1
df.style.apply(color, axis=None).to_excel('styled.xlsx', engine='openpyxl', index=False)