在熊猫和xlsxwriter中具有不同索引的问题

问题描述 投票:0回答:1

这里是可以正常工作的代码:

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()

这将在下面创建输出。

enter image description here

我的问题是,是否可以在熊猫索引中包含标头Data?我想从第一行开始索引。所以标题Data应该具有索引0。这很有用,因为在xlsxwriter中,第一行的索引为0。

这是运行良好的代码:将pandas导入为pd#从某些数据创建Pandas数据框。 df = pd.DataFrame({'Data':[10,20,30,20,15,30,45]})#使用...

python python-3.x pandas indexing xlsxwriter
1个回答
0
投票

首先索引是一个对象,默认索引从0开始。您可以通过键入以下内容将其快速移动:

© www.soinside.com 2019 - 2024. All rights reserved.