【发布时间】:2020-03-13 13:35:38
【问题描述】:
下面是运行良好的代码:
import pandas as pd
# Create a Pandas dataframe from some data.
df = pd.DataFrame({'Data': [10, 20, 30, 20, 15, 30, 45]})
# 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(1,1,1,1, {'type': '3_color_scale'}) ##CHANGES THE COLOR OF SECOND ROW
# Close the Pandas Excel writer and output the Excel file.
writer.save()
这将创建以下输出。
我的问题是,是否可以在 pandas 索引中包含 Header Data?我想从第一行开始索引。所以标题Data 应该有索引0。这很有用,因为在xlsxwriter 第一行有索引0。
【问题讨论】:
标签: python python-3.x pandas indexing xlsxwriter