如何在 pandas 数据框中重置索引? [英] How to reset index in a pandas dataframe?
本文介绍了如何在 pandas 数据框中重置索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数据框,我从其中删除了一些行.结果,我得到一个数据帧,其中的索引是这样的:[1,5,6,10,11]
,我想将其重置为[0,1,2,3,4]
.我该怎么办?
I have a dataframe from which I remove some rows. As a result, I get a dataframe 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?
以下似乎有效:
df = df.reset_index()
del df['index']
以下内容无效:
df = df.reindex()
推荐答案
reset_index()
是您所需要的.如果您不希望将其另存为列,请执行以下操作:
reset_index()
is what you're looking for. If you don't want it saved as a column, then do:
df = df.reset_index(drop=True)
如果您不想重新分配:
df.reset_index(drop=True, inplace=True)
这篇关于如何在 pandas 数据框中重置索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文