[英]Write dataframe to excel with a title
我想在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=0
, startrow=0
)。
如何使用ExcelWriter添加字符串標題?
您應該能夠使用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()
這樣就可以了:
In[16]: sheet = writer.sheets['Sheet1'] #change this to your own
In[17]: sheet.write(0,0,"My documentation text")
In[18]: writer.save()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.