使用标题将数据帧写入excel

Ham*_*med 4 python excel xlsx dataframe pandas

我想在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()
Run Code Online (Sandbox Code Playgroud)

这产生了我需要的东西,但另外我想在表格(startcol=0,startrow=0)上添加一些带有一些文本(解释)的标题.

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

Fab*_*nna 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()
Run Code Online (Sandbox Code Playgroud)


Yan*_* P. 5

这将解决问题:

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