避免在 pandas to_excel方法中合并单元格 [英] Avoid merged cells in pandas to_excel method
问题描述
我有一个多索引数据框,必须另存为excel文件.当我使用pandas方法"to_excel"来执行此操作时,将得到一个包含合并单元格的漂亮表.这是一个这样的表格的示例:
I've got a multiindex dataframe which I have to save as an excel file. When I use pandas method "to_excel" to do so, I get a nice table which incorporates merged cells. Here is an example of how such a table looks like:
不幸的是,在excel中过滤此表的第一列是非常成问题的,因为excel无法理解合并的单元格属于同一类: https://www.extendoffice.com/documents/excel /1955-excel-filter-merged-cells.html
Unfortunately, filtering the first column of this table is very problematic in excel since excel does not understand that the merged cells belong together: https://www.extendoffice.com/documents/excel/1955-excel-filter-merged-cells.html
这就是为什么我需要'to_excel'方法来保存数据框的原因:
That's why I need the 'to_excel' method to save the dataframe like that:
有可能吗?
顺便说一下,这就是我用来生成第一个表的代码:
By the way, that's the code which I used to produce the first table:
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")
推荐答案
使用 merge_cells=False
参数:
mydf.to_excel("some_path_here", merge_cells=False)
来自文档:
merge_cells :布尔值,默认为True
merge_cells : boolean, default True
将MultiIndex和Hierarchical Rows写为合并的单元格.
Write MultiIndex and Hierarchical Rows as merged cells.
这篇关于避免在 pandas to_excel方法中合并单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!