python - 调用 StyleFrame.apply_style_by_indexes 时如何避免 ValueError?
问题描述
目前我有一个很大的 StyleFrame 对象列表。来自某些数据帧的一些单元格我想根据它们的值为背景着色:> 1 为红色,<=1 为绿色。我认为多次调用“apply_style_by_indexes”方法会创建多个命名样式,我不知道如何避免它。
red_shade = "F85C4D"
green_shade = "90EE90"
for key in dataframe_dict_keys:
sf.apply_style_by_indexes(sf[sf[key] <= threshold],
styler_obj=Styler(bg_color=green_shade),
cols_to_style=key)
sf.apply_style_by_indexes(sf[sf[key] > threshold],
styler_obj=Styler(bg_color=red_shade),
cols_to_style=key)
调用“.to_excel”方法时引发错误,该方法为多个 StyleFrame 对象调用
dic["dataframe"].to_excel(writer, sheet_name=country_code, startrow=idx, index=True, best_fit=list(dic["keys"]))
编辑:
def __apply_colors_based_on_value(self, sf, dataframe_dict_keys, threshold):
for key in dataframe_dict_keys:
sf.apply_style_by_indexes(sf[sf[key] <= threshold],
styler_obj=Styler(bg_color="F85C4D",
cols_to_style=key)
sf.apply_style_by_indexes(sf[sf[key] > threshold],
styler_obj=Styler(bg_color="F85C4D",
cols_to_style=key)
WHERE: sf - StyleFrame 实例,dataframe_dict_keys - StyleFrame 的列(标题)列表, dic - dict{“dataframe”:StyleFrame 对象,“keys”:List[str] # 标题列
解决方案
推荐阅读
- sql - 如何递归地从实体框架中的父/子获取 id?
- javascript - Vuejs 使用 postmessage 从 iframe 接收事件
- multithreading - 线程被台面信号量唤醒后如何获取锁?
- wpf - 为什么 DisplayMemberPath 没有显示?
- deep-learning - CNN_如何使用不同大小的输入输出图像?
- python - 在对二维数组中的奇数行和偶数行和列进行分组时跳过一列
- angular - 从另一个组件调用一个角度组件
- git - 将选定的提交历史推送到 github
- powershell - 使用 Powershell 定位和更新文本文件中的键值
- javascript - 我的石头、纸、剪刀游戏的 JavaScript 代码不工作