read.excel标头上的Pandas错误的原因是什么?=无,多个index_col

时间:2019-08-08 10:21:09

标签: python excel pandas dataframe

这与以下SO问题有关:read_excel in pandas giving error for no header and multiple index_col's

但是我想知道为什么会这样,而不是一种解决方法。数据框:

enter image description here

数据:

var words = [
  "Hello",
  "Hi",
  "Where",
  "Which",
  "Who",
  "When",
  "What",
  "Come",
  "Get",
  "Post"
]

var newArr = words.map(word => {
    return {word:word,explain:"Say " + word}
})

读取数据,例如:

{0: {0: nan, 1: nan, 2: nan, 3: 'A', 4: 'A', 5: 'B', 6: 'B', 7: 'C', 8: 'C'},
 1: {0: nan, 1: nan, 2: nan, 3: 1.0, 4: 2.0, 5: 1.0, 6: 2.0, 7: 1.0, 8: 2.0},
 2: {0: 'AA1', 1: 'a', 2: 'ng/mL', 3: 1, 4: 1, 5: 1, 6: 1, 7: 1, 8: 1},
 3: {0: 'AA2', 1: 'a', 2: nan, 3: 1, 4: 1, 5: 1, 6: 1, 7: 1, 8: 1},
 4: {0: 'BB1', 1: 'b', 2: nan, 3: 1, 4: 1, 5: 1, 6: 1, 7: 1, 8: 1},
 5: {0: 'BB2', 1: 'b', 2: 'mL', 3: 1, 4: 1, 5: 1, 6: 1, 7: 1, 8: 1},
 6: {0: 'CC1', 1: 'c', 2: nan, 3: 1, 4: 1, 5: 1, 6: 1, 7: 1, 8: 1},
 7: {0: 'CC2', 1: 'c', 2: nan, 3: 1, 4: 1, 5: 1, 6: 1, 7: 1, 8: 1}}

不起作用:

pd.read_excel(file_path, skiprows=3, index_col=[0, 1], header=None)

为什么?

1 个答案:

答案 0 :(得分:0)

在完整的追溯中给出了解释:

  ....
  File "D:\Programme\Python36\lib\site-packages\pandas\io\excel\_base.py", line 473, in parse offset = 1 + header
  TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'

header设置为None,并且在计算偏移量时会尝试将1添加到None,从而产生TypeError。我认为这只是一个错误。

以下内容绝对没有任何保证:
... \ Lib \ site-packages \ pandas \ io \ excel_base.py的第473行应从offset = 1 + header更改为offset = 1 + header if header is not None else -1,以使多个索引列可与header=None