首页 > 解决方案 > 使用 Pandas 库的 Python Excel 样式

问题描述

我正在编写一个学校项目 rn,我必须分析股票并预测它们。所以现在我的问题是代码已经完成,但我想在我的 Excel 文件中添加颜色。

我正在使用以下库:

所以我查遍了整个互联网,发现没有任何帮助。这是我编写 Excel 文件的代码:

def write_xlsx():
    help = []
    for i in range(len(new_price)):
        list = []
        list.append(new_price[i])
        list.append(price_dif[i])
        list.append(high[i])
        list.append(low[i])
        list.append(keepsell[i])
        help.append(list)
    list = [datetime.now(), None, None, None, None]
    help.append(list)
    df1 = pd.DataFrame(help, index=[get_names()], columns=['Price (€)', 'Difference (€)', 'High (€)', 'Low (€)', 'Prediction'])
    try:
        df1.to_excel("Stock.xlsx", sheet_name='Stonks')
    except:
        print("Please close the Exel Document within 15 seconds!")
        time.sleep(15)
        df1.to_excel("Stock.xlsx", sheet_name='Stonks')

我想为我的价格差异或高|低列着色。你能帮我如何为Excel文件着色吗?

如果您需要更多代码,请告诉我。

标签: pythonpandascolors

解决方案


请参阅 Stack Overflow 中的这些帖子:

参考资料 1:Pandas 中的着色细胞

参考 2:使用 python/pandas 在 excel 上创建颜色渐变的最简单方法?


推荐阅读