如何重置大 pandas 数据框架中的索引? [英] How to reset index in a pandas data frame?

查看:122
本文介绍了如何重置大 pandas 数据框架中的索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框架,我从中删除一些行。因此,我得到一个数据框架,其中索引是这样的: [1,5,6,10,11] ,我想将其重置为 [0,1,2,3,4] 。如何做?



ADDED



以下内容似乎有效:

  df = df.reset_index()
del df ['index']

以下内容不起作用:

  df = df.reindex()


解决方案

reset_index()是您要查找的内容。如果您不想将其另存为列,则

  df = df.reset_index(drop = True)


I have a data frame from which I remove some rows. As a result, I get a data frame in which index is something like that: [1,5,6,10,11] and I would like to reset it to [0,1,2,3,4]. How can I do it?

ADDED

The following seems to work:

df = df.reset_index()
del df['index']

The following does not work:

df = df.reindex()

解决方案

reset_index() is what you're looking for. if you don't want it saved as a column, then

df = df.reset_index(drop=True)

这篇关于如何重置大 pandas 数据框架中的索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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