使用标题将数据帧写入excel

时间:2016-01-13 13:11:18

标签: python excel pandas dataframe xlsx

我想在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添加字符串标题?

2 个答案:

答案 0 :(得分:6)

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

答案 1 :(得分:4)

这样可以解决问题:

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