因此,我有以下变量:
text1 = "some text here"
text2 = "other text here"
df = pd.DataFrame({"a": [1,2,3,4,5], "b": [6,7,8,9,10], "c": [11,12,13,14,15]})
Run Code Online (Sandbox Code Playgroud)
正如我所知,有可能使用xlsxwriter来执行此操作,这意味着我基本上必须遍历整个数据帧,以将每个条目写入excel工作簿中的不同单元格.这非常麻烦.
所以,我认为必须有一个更简单的方法来做到这一点; 这样的事情:
writer = pd.ExcelWriter("test.xlsx", engine="xlsxwriter")
writer.write(text1, startrow=0, startcol=0)
writer.write(text1, startrow=1, startcol=0)
df.to_excel(writer, startrow=4, startcol=0)
Run Code Online (Sandbox Code Playgroud)
有没有更简单的方法?
我有一个多索引数据框,我必须另存为excel文件。当我使用pandas方法“ to_excel”来执行此操作时,将得到一个包含合并单元格的漂亮表。这是一个这样的表格的示例:

不幸的是,在excel中过滤此表的第一列是非常成问题的,因为excel无法理解合并的单元格是属于一起的:https : //www.extendoffice.com/documents/excel/1955-excel-filter-merged-cells。 html
这就是为什么我需要'to_excel'方法来保存数据框的原因:

那可能吗?
顺便说一下,这就是我用来生成第一个表的代码:
df = pd.DataFrame({"animal": ("horse", "horse", "dog", "dog"), "color of fur": ("black", "white", "grey", "black"), "name": ("Blacky", "Wendy", "Rufus", "Catchy")})
mydf = df.set_index(["animal", "color of fur"])
mydf.to_excel("some_path_here")
Run Code Online (Sandbox Code Playgroud)