我正在处理一个数据框(来自 Excel 文件),我想格式化单元格。
DF具有以下内容(作为示例):
a |
b |
c |
234 --> 500 |
200 --> 500 |
0 |
234 --> 500 |
400 |
6 |
66 |
34 |
1 |
我想将包含“-->”的单元格着色为红色。有什么建议么?我知道 Pandas 有样式功能,但我找不到任何可以帮助我解决问题的东西。
您可以修改负色红色 https://pandas.pydata.org/docs/user_guide/style.html#Building-styles测试函数是否存在减去in
:
def make_your_style(val):
"""
Takes a scalar and returns a string with
the css property `'background-color: red'` for string with -->
"""
color = 'red' if '-->' in str(val) else ''
return f'background-color: {color}'
df.style.applymap(make_your_style)
对于写入 Excel 使用:
df.style.applymap(make_your_style).to_excel(file, engine='openpyxl', index=False)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)