2016-01-13 53 views
3

我想在Excel中打印出一个数据框。我使用ExcelWriter如下:将数据框写入excel以标题

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

回答

5

你应该能够编写与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() 
+0

感谢百万法比奥。我试图使用write_string,但得到了一些错误的错误实现。现在它完美地工作。 – Hamed

3

这将这样的伎俩:

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

谢谢Yannis对你的评论。干杯。 – Hamed