使用标题将数据框写入 excel

Write dataframe to excel with a title

我想在 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()

这产生了我需要的东西,但另外我想为 table 顶部的数据添加一个带有一些文本(解释)的标题(startcol=0 ,startrow=0 ).

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

这样做就可以了:

In[16]: sheet = writer.sheets['Sheet1'] #change this to your own
In[17]: sheet.write(0,0,"My documentation text")
In[18]: writer.save()

您应该能够使用 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()