Write dataframe to excel with a title

别说谁变了你拦得住时间么 提交于 2019-12-07 10:40:45

问题


I would like to print out a dataframe in Excel. I am using ExcelWriter as follows:

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()

This produces what I need but in addition I would like to add a title with some text (explanations) for the data on top of the table (startcol=0 ,startrow=0).

How can I add a string title using ExcelWriter?


回答1:


You should be able to write text in a cell with the write_string method, adding some reference to XlsxWriter to your code:

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()



回答2:


This will do the trick:

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


来源:https://stackoverflow.com/questions/34767635/write-dataframe-to-excel-with-a-title

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!