如何在一个to_excel()和一个read_excel()之间保留一个大 pandas 多索引? [英] How can I preserve a pandas multi-index between a to_excel() and a read_excel()?
问题描述
根据 read_excel 的大熊猫文档, / a>,我可以将索引列名称放在一行上,然后该方法将哪些列用作索引。 我想从多索引的数据帧创建一个可以读取的Excel文件,但是我无法弄清楚如何获取大熊猫写的 to_excel ,以便创建此附加行(来自多索引数据帧)。 I want to create an Excel file from a multi-indexed dataframe that can be read in as such, but I can't figure out how to get pandas to write to_excel in such a way that this additional row is created (from a multi-indexed dataframe). 我无法想象将多索引数据框存储为Excel工作表,然后在以后将其拉回来是不常见的一个用例,所以我想知道如果我只是避开'想知道如何做到这一点。 I can't imagine that storing a multi-indexed dataframe as an Excel worksheet and then pulling it back in later is that uncommon a use case, so I'm wondering if I just haven't figured out how to do this. 这是一个数据框的例子,我想在readi之前在冻结Excel不需要告诉read_excel哪些列是索引: Here's an example of a dataframe I'd like to 'freeze' in Excel before reading back in without having to tell read_excel which columns are the indices: 在我的例子中,有三个索引列:Rec,Section和Row。 In my example, there are three index columns: Rec, Section, and Row. 当我将其写入Excel然后阅读它时,不想说这个。因为read_excel似乎有一个方法,当它们出现在一个单独的行上时会推断索引名称,所以我想让它只是想出来(假设我正确地写了Excel文件)。 When I write this to Excel and then read it back in, I don't want to have to tell it this. Since read_excel seems to have a method that infers the index names when they appear on a separate row, I want to have it just figure it out (assuming I correctly write the Excel file). 我缺少什么? 当尝试将数据透视表写入Excel时,遇到同样的问题。我可以通过修改 I was encountering the same issue when trying to write a pivot table to Excel. I was able to get this to work by modifying the 由于此功能仍然不支持 As this functionality is still not supported by 我在他的 GitHub评论。 这篇关于如何在一个to_excel()和一个read_excel()之间保留一个大 pandas 多索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!ipdb> my_df
Date Amount
Rec Section Row
0 Top Section 2 2015-05-01 -105.00
1 Middle Section 3 2015-05-04 90247.60
2 Middle Section 4 2015-05-05 -2992.99
3 Bottom Section 5 2015-05-08 -800.00
推荐答案
../ pandas / core
中的 frame.py
文件来使其工作。如果self.columns.nlevels>更改 1
to if self.columns.nlevels>
frame.py
file in ../pandas/core
. Changing if self.columns.nlevels > 1
to if self.columns.nlevels > 1 and not index
got me what I needed.熊猫
,你可能还会遇到有趣的输出。此外,这也可能不会解决 read_excel
的问题。希望这有一点帮助!Pandas
, you may still encounter funny output. Also, this will likely not solve the issue for read_excel
either. Hopefully this helps a little!