简体   繁体   English

pandas.read_excel()与标题行列表

[英]pandas.read_excel() with list of header rows

I'm trying to parse an Excel file with a table which has two header rows looking like: 我正在尝试使用具有两个标题行的表来解析Excel文件:

| Category A | Category B |
|  1   |  2  |  1   |  2  |

According to the manual I can pass a list of ints to specify rows to use for a multi-index, but when I do: 根据手册,我可以传递一个整数列表来指定要用于多索引的行,但是当我这样做时:

pd.read_excel(path, header = [1,2])

I get back an error saying "list indices must be integers, not list". 我回来了一个错误,说“列表索引必须是整数,而不是列表”。 Apparently I'm not allowed to put any list as header kwarg, as I get the same error when doing header = [1] (as opposed to header=1 , which works fine). 显然,我不允许将任何列表作为header kwarg放置,因为在执行header = [1]时会遇到相同的错误(与header=1相对,这很好用)。

What am I doing wrong here? 我在这里做错了什么?

It sounds like you might not have the Pandas 0.17.0 installed (the man page you linked to). 听起来您可能未安装Pandas 0.17.0(链接到的手册页)。

The read_csv function has been changed - Pandas 0.14.0 (for example) didn't allow a list of ints to be passed as a header argument, see here . read_csv函数已更改-例如,熊猫0.14.0不允许将整数列表作为标头参数传递, 请参见此处

First, the rows you put into header keyword should be 0-indexed, so you probably want header=[0,1] ? 首先,您放入header关键字的行应为0索引,因此您可能希望header=[0,1]

Second, there was a bug if the multi-index columns contained numeric values, which will be fixed in 0.17.1 (coming soon), or you can build from master - https://github.com/pydata/pandas/pull/11328 其次,如果多索引列包含数字值(这将在0.17.1中修复)(即将推出),或者您可以从母版进行构建-https: //github.com/pydata/pandas/pull/ 11328

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM