Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python/pandas在excel上创建颜色渐变的最简单方法?_Python_Excel_Pandas - Fatal编程技术网

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

使用python/pandas在excel上创建颜色渐变的最简单方法?,python,excel,pandas,Python,Excel,Pandas,因此,我有数据,我正在输出到一个excel文件使用熊猫的ExcelWriter。将整个数据输出到Excel文件后,使用Python以编程方式对其应用条件格式的最简单方法是什么 我希望能够(通过Python)选择(在Excel中)Excel工作表中的所有填充单元格,然后单击“条件格式”>色阶。最终的结果是基于这些值的颜色渐变,如果你愿意的话,就是“热图” 以下是我生成数据的步骤: writer = ExcelWriter('Data' + today +'.xls') ... processing

因此,我有数据,我正在输出到一个excel文件使用熊猫的ExcelWriter。将整个数据输出到Excel文件后,使用Python以编程方式对其应用条件格式的最简单方法是什么

我希望能够(通过Python)选择(在Excel中)Excel工作表中的所有填充单元格,然后单击“条件格式”>色阶。最终的结果是基于这些值的颜色渐变,如果你愿意的话,就是“热图”

以下是我生成数据的步骤:

writer = ExcelWriter('Data' + today +'.xls')
... processing data ... 
df.to_excel(writer, sheet_name = 'Models', startrow = start_row, index=False)

在写入数据之后,我需要一种使用python应用条件格式的方法。为了简单起见,我希望颜色越深,蓝色越深,值越正(>0),红色越深,值越负(以下是如何将条件格式应用于Pandas创建的XlsxWriter Excel文件的示例:

import pandas as pd

# Some sample data to plot.
list_data = [30, 40, 50, 40, 20, 10, 5]

# Create a Pandas dataframe from the data.
df = pd.DataFrame(list_data)

# Create a Pandas Excel writer using XlsxWriter as the engine.
excel_file = 'testfile.xlsx'
sheet_name = 'Sheet1'

writer = pd.ExcelWriter(excel_file, engine='xlsxwriter')
df.to_excel(writer, sheet_name=sheet_name)

# Access the XlsxWriter workbook and worksheet objects from the dataframe.
# This is equivalent to the following using XlsxWriter on its own:
#
#    workbook = xlsxwriter.Workbook('filename.xlsx')
#    worksheet = workbook.add_worksheet()
#
workbook = writer.book
worksheet = writer.sheets[sheet_name]

# 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()
输出如下所示:

请参阅上的XlsxWriter文档,了解如何更改颜色或其他属性


另请参见。

谢谢先生/女士的服务。我该如何指定我想要的颜色?@jenny。设置颜色和其他属性在上面链接的文档中有说明。我的错,我找到了。我找到了。谢谢。