2016-01-13 127 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