简体   繁体   English

熊猫在重复 DataFrame.reset_index() 上崩溃

[英]pandas crashes on repeated DataFrame.reset_index()

Very weird bug here: I'm using pandas to merge several dataframes.这里非常奇怪的错误:我正在使用熊猫来合并多个数据帧。 As part of the merge, I have to call reset_index several times.作为合并的一部分,我必须多次调用 reset_index。 But when I do, it crashes unexpectedly on the second or third use of reset_index.但是当我这样做时,它在第二次或第三次使用 reset_index 时意外崩溃。

Here's minimal code to reproduce the error:这是重现错误的最少代码:

import pandas
A = pandas.DataFrame({
    'val' :  ['aaaaa', 'acaca', 'ddddd', 'zzzzz'],
    'extra' : range(10,14),
})
A = A.reset_index()
A = A.reset_index()
A = A.reset_index()

Here's the relevant part of the traceback:这是回溯的相关部分:

....
    A = A.reset_index()
  File "/usr/local/lib/python2.7/dist-packages/pandas/core/frame.py", line 2393, in reset_index
    new_obj.insert(0, name, _maybe_cast(self.index.values))
  File "/usr/local/lib/python2.7/dist-packages/pandas/core/frame.py", line 1787, in insert
    self._data.insert(loc, column, value)
  File "/usr/local/lib/python2.7/dist-packages/pandas/core/internals.py", line 893, in insert
    raise Exception('cannot insert %s, already exists' % item)
Exception: cannot insert level_0, already exists

Any idea what's going wrong here?知道这里出了什么问题吗? How do I work around it?我该如何解决?

Inspecting frame.py, it looks like pandas tries to insert a column 'index' or 'level_0'.检查frame.py,它看起来像pandas 试图插入列“index”或“level_0”。 If either/both(??) of them are already taken, then it throws the error.如果它们中的一个/两个(??)已经被采用,那么它会抛出错误。

Fortunately, there's a "drop" option.幸运的是,有一个“丢弃”选项。 AFAICT, this drops an existing index with the same name and replaces it with the new, reset index. AFAICT,这会删除具有相同名称的现有索引并将其替换为新的重置索引。 This might get you in trouble if you have a column named "index," but I think otherwise you're okay.如果您有一个名为“index”的列,这可能会给您带来麻烦,但我认为否则您没问题。

"Fixed" code: “固定”代码:

import pandas
A = pandas.DataFrame({
    'val' :  ['aaaaa', 'acaca', 'ddddd', 'zzzzz'],
    'extra' : range(10,14),
})
A = A.reset_index(drop=True)
A = A.reset_index(drop=True)
A = A.reset_index(drop=True)

您可以使用 :

A.reset_index(drop=True, inplace=True)

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

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