我想在Excel中打印出一个数据框。我正在使用ExcelWriter,如下所示:

writer = pd.ExcelWriter('test.xlsx')
df = DataFrame(C,ind)    # C is the matrix and ind is the list of corresponding indices
df.to_excel(writer, startcol = 0, startrow = 5)
writer.save()

这产生了我需要的东西,但除此之外,我想在表顶部(startcol=0startrow=0)的顶部添加一些带有文本(解释)的标题。

如何使用ExcelWriter添加字符串标题?

最佳答案

您应该能够使用write_string方法在单元格中编写文本,并在代码中添加一些对XlsxWriter的引用:

writer = pd.ExcelWriter('test.xlsx')
df = DataFrame(C,ind)    # C is the matrix and ind is the list of corresponding indices
df.to_excel(writer, startcol = 0, startrow = 5)

worksheet = writer.sheets['Sheet1']
worksheet.write_string(0, 0, 'Your text here')

writer.save()

10-07 19:32
查看更多