pandas 重置指数似乎不起作用? [英] Pandas reset index doesn't seem to work?
问题描述
我不确定我在哪里误入歧途,但我似乎无法重置数据帧上的索引。
I'm not sure where I am astray but I cannot seem to reset the index on a dataframe.
当我运行测试时。 head()
,我得到以下输出:
When I run test.head()
, I get the output below:
如您所见,数据帧是一个切片,因此索引超出范围。
我想要做的是重置此数据帧的索引。所以我运行 test.reset_index(drop = True)
。这输出如下:
As you can see, the dataframe is a slice, so the index is out of bounds.
What I'd like to do is to reset the index for this dataframe. So I run test.reset_index(drop=True)
. This outputs the following:
这看起来像是一个新的索引,但事实并非如此。再次运行 test.head
,索引仍然相同。尝试使用 lambda.apply
或 iterrows()
会导致数据帧出现问题。
That looks like a new index, but it's not. Running test.head
again, the index is still the same. Attempting to use lambda.apply
or iterrows()
creates problems with the dataframe.
我怎样才能真正重置索引?
How can I really reset the index?
推荐答案
BrenBarn的回答有效。
BrenBarn's answer works.
以下内容也适用于这个主题,这不是故障排除,而是如何重置索引:
The following also worked via this thread, which isn't a troubleshooting so much as an articulation of how to reset the index:
test = test.reset_index()
del test['index']
这篇关于 pandas 重置指数似乎不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!