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

查看:43
本文介绍了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.applyiterrows() 会导致数据帧出现问题.

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天全站免登陆