用标题编写数据框以表现出色 [英] Write dataframe to excel with a title

查看:55
本文介绍了用标题编写数据框以表现出色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Excel中打印出一个数据框.我使用ExcelWriter的方法如下:

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

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

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

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

How can I add a string title using ExcelWriter?

推荐答案

您应该能够使用 write_string 方法,在代码中添加了对 XlsxWriter 的一些引用:

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

这篇关于用标题编写数据框以表现出色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆