【发布时间】:2017-12-08 11:57:27
【问题描述】:
如何在 C 列中将低于 1 的单元格着色为绿色,将 0 到 0.5 之间的单元格着色为黄色。
当前输出:
A B C
0 TT WW -5
1 AA WW 0
2 DD WW 5
期望:
以下代码对我来说只是写入 Excel,没有颜色变化。
import pandas as pd
import numpy as np
df = pd.read_excel("C:\\13\\13\\13.xlsx")
def highlight_vals(val, min=-1111, max=1, color='green'):
if min < val < max:
return 'background-color: %s' % color
else:
return ''
df.style.applymap(highlight_vals, subset=['C'])
def highlight_vals(val, min=0, max=0.5, color='yellow'):
if min < val < max:
return 'background-color: %s' % color
else:
return ''
df.style.applymap(highlight_vals, subset=['C'])
writer = pd.ExcelWriter("C:\\13\\13\\zz.xlsx")
df.to_excel(writer, startrow=0, startcol=0, index = False)
writer.save()
【问题讨论】:
标签: python pandas dataframe conditional-formatting