gpt4 book ai didi

python - python 上的 xlsxwriter,使用条件格式函数和 '3_color_scale'

转载 作者:太空宇宙 更新时间:2023-11-03 14:57:59 24 4
gpt4 key购买 nike

我有一个 pandas DataFrame,我将其写入 Excel 工作表。然后我想对其进行格式化,以便它显示该 DataFrame 的热图。

我认为解决此问题的最佳方法是使用 worksheet.conditional_format() 函数和 {'type': '3_color_scale'} 作为我的参数.

但是,我注意到这仅在保存整数的单元格中着色。它不会给任何有漂浮物的东西着色。有谁知道如何解决这个问题吗?

这是一个代码示例。

writer = pd.ExcelWriter(file_name, engine='xlsxwriter')
df.to_excel(writer, sheet_name=selected_factor, startrow=row_location, startcol=col_location)
worksheet = writer.sheets['Sheet 1 testing']
worksheet.conditional_format('B8:E17',{'type': '3_color_scale'})

我使用的是 Python 2.7 并使用 pandas 版本 0.15.2

编辑

我明白了为什么它给我带来了麻烦,数字被作为字符串保存到电子表格中,而不是作为 float 。

最佳答案

当我运行以下示例时,我没有看到此问题。它突出显示整数和 float :

import pandas as pd

# Create a Pandas dataframe from some data.
df = pd.DataFrame({'Data': [10, 20.5, 30, 40, 50.7, 62, 70]})

# Create a Pandas Excel writer using XlsxWriter as the engine.
writer = pd.ExcelWriter('pandas_conditional.xlsx', engine='xlsxwriter')

# Convert the dataframe to an XlsxWriter Excel object.
df.to_excel(writer, sheet_name='Sheet1')

# Get the xlsxwriter workbook and worksheet objects.
workbook = writer.book
worksheet = writer.sheets['Sheet1']

# Apply a conditional format to the cell range.
worksheet.conditional_format('B2:B8', {'type': '3_color_scale'})

# Close the Pandas Excel writer and output the Excel file.
writer.save()

输出:

enter image description here

关于python - python 上的 xlsxwriter,使用条件格式函数和 '3_color_scale',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45361170/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com