Python 如何重置熊猫数据框中的索引?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20490274/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to reset index in a pandas dataframe?
提问by Roman
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?
我有一个数据框,从中删除了一些行。结果,我得到了一个数据帧,其中的索引类似于:[1,5,6,10,11]并且我想将其重置为[0,1,2,3,4]. 我该怎么做?
The following seems to work:
以下似乎有效:
df = df.reset_index()
del df['index']
The following does not work:
以下不起作用:
df = df.reindex()
采纳答案by mkln
reset_index()is what you're looking for. If you don't want it saved as a column, then do:
reset_index()就是你要找的。如果您不想将其保存为列,请执行以下操作:
df = df.reset_index(drop=True)
If you don't want to reassign:
如果您不想重新分配:
df.reset_index(drop=True, inplace=True)
回答by jezrael
Another solutions are assign RangeIndexor range:
另一种解决方案是分配RangeIndex或range:
df.index = pd.RangeIndex(len(df.index))
df.index = range(len(df.index))
It is faster:
它更快:
df = pd.DataFrame({'a':[8,7], 'c':[2,4]}, index=[7,8])
df = pd.concat([df]*10000)
print (df.head())
In [298]: %timeit df1 = df.reset_index(drop=True)
The slowest run took 7.26 times longer than the fastest. This could mean that an intermediate result is being cached.
10000 loops, best of 3: 105 μs per loop
In [299]: %timeit df.index = pd.RangeIndex(len(df.index))
The slowest run took 15.05 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 7.84 μs per loop
In [300]: %timeit df.index = range(len(df.index))
The slowest run took 7.10 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 14.2 μs per loop
回答by user10692571
data1.reset_index(inplace=True)

