创建多个工作表然后填充pandas df结果的正确方法是什么?

时间:2019-06-14 17:59:25

标签: xlsxwriter

我需要按特定顺序创建几个工作表。

当我致电dataframe.to_excel(writer, sheet_name='Sales and Labor Rpt', startrow=startrow, startcol=1)

这是我想做的,但是在dataframe.to_excel()失败了:

sheet_names = ['Sales and Labor Rpt', 'Sales Data', 'TimeClock Data']
sheet_list={}
for sheet_name in sheet_names:
    **sheet_list[sheet_name] = workbook.add_worksheet(sheet_name)**

sheet_list['Sales and Labor Rpt'].write_string(1, 1, "Sales and Labor Report")

# Convert the dataframe to an XlsxWriter Excel object.
startrow = 4
dataframe_list = [all_sales_totals, departmental_sales_totals, labor_dollars_by_department, labor_hours_by_department]
for dataframe in dataframe_list:
    dataframe.to_excel(writer, sheet_name='Sales and Labor Rpt', startrow=startrow, startcol=1)
    startrow = startrow + dataframe.shape[0] + 5

writer.save()

docs表示它应在多次调用时起作用:

# Position the dataframes in the worksheet.
df1.to_excel(writer, sheet_name='Sheet1')  # Default position, cell A1.
df2.to_excel(writer, sheet_name='Sheet1', startcol=3)
df3.to_excel(writer, sheet_name='Sheet1', startrow=6)

# Write the dataframe without the header and index.
df4.to_excel(writer, sheet_name='Sheet1',
             startrow=7, startcol=4, header=False, index=False)

0 个答案:

没有答案