How to reset index in a pandas dataframe? [duplicate]

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?


The following seems to work:

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

The following does not work:

df = df.reindex() 
1

3 Answers

DataFrame.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) 

If you don't want to reassign:

df.reset_index(drop=True, inplace=True) 
6

Another solutions are assign RangeIndex or 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 
3
data1.reset_index(inplace=True) 

You Might Also Like