pandas 重置索引未生效 [英] Pandas reset index is not taking effect

查看:79
本文介绍了 pandas 重置索引未生效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定自己在哪里误入歧途,但似乎无法重置数据框上的索引.

I'm not sure where I am astray but I cannot seem to reset the index on a dataframe.

当我运行 test.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的 answer 可行.

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(drop=True)

这篇关于 pandas 重置索引未生效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆