475

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()
Scott Boston
  • 133,446
  • 13
  • 126
  • 161
Roman
  • 112,185
  • 158
  • 335
  • 439

3 Answers3

963

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)
Shubham Sharma
  • 52,812
  • 6
  • 20
  • 45
mkln
  • 12,615
  • 4
  • 17
  • 22
  • 117
    Instead of reassign the dataframe to the same variable you can set `inplace=True` argument. – alhuelamo Feb 24 '16 at 13:03
  • 39
    Note that in case of `inplace=True` the method returns None – alyaxey Oct 30 '17 at 10:41
  • This solved my problem of incurring in ValueError: cannot insert level_0, already exists while using df = df.reset_index() multiple times – Tms91 Nov 17 '19 at 15:25
  • @mkln, I have increased in both columns (even with `drop =True`) and rows. `df.reset_index()` only fixed the rows. Can be used to fix the columns as well? – Amir Jan 27 '21 at 19:17
  • Why would we want to reset the index if we are going to drop it anyway? – Victor May 12 '21 at 00:44
  • @Victor - If you don't "drop" the index, it will add a new index, and save the old index values as a series in your dataframe – Caleb McNevin Jun 03 '21 at 17:10
65

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
jezrael
  • 729,927
  • 78
  • 1,141
  • 1,090
  • 2
    @Outcast Source - The fastest is `len(df.index)`, 381ns vs `df.shape` 1.17us. Oyr something missing? – jezrael Jan 03 '18 at 05:15
  • 1
    This is an elegant solution to reset the index. Thank you! I found out that if you try to convert an hdf5 object to pandas.DataFrame object, you have to reset the index before you can edit certain sections of the DataFrame. – troymyname00 Jun 16 '19 at 12:38
  • Does the timing change much if you do `df.reset_index(drop=True, inplace=True)` to avoid the copy? – Cole Mar 27 '22 at 13:54
18
data1.reset_index(inplace=True)
rsc
  • 9,841
  • 4
  • 35
  • 34
user10692571
  • 199
  • 1
  • 2