23

I have a data frame. Then I have a logical condition using which I create another data frame by removing some rows. The new data frame however skips indices for removed rows. How can I get it to reindex sequentially without skipping? Here's a sample coded to clarify

import pandas as pd
import numpy as np

jjarray = np.array(range(5))
eq2 = jjarray == 2
neq2 = np.logical_not(eq2)

jjdf = pd.DataFrame(jjarray)
jjdfno2 = jjdf[neq2]

jjdfno2

Out:

  0
0 0
1 1
3 3
4 4

I want it to look like this:

  0
0 0
1 1
2 3
3 4

Thanks.

JJJ
  • 1,009
  • 6
  • 19
  • 31
user2133151
  • 247
  • 1
  • 2
  • 10

1 Answers1

42

One way is to use reset_index:

>>> df = pd.DataFrame(range(5))
>>> eq2 = df[0] == 2
>>> df_no_2 = df[~eq2]
>>> df_no_2
   0
0  0
1  1
3  3
4  4
>>> df_no_2.reset_index(drop=True)
   0
0  0
1  1
2  3
3  4
DSM
  • 342,061
  • 65
  • 592
  • 494
  • 7
    I believe you also need to specify `inplace=True`, otherwise `df_no_2` will not be affected and a new data frame instead will be created: `df_no_2.reset_index(drop=True, inplace=True)` (pandas v0.15.2) – silentser Jan 22 '15 at 12:31
  • 1
    Usually you simply just assign the result to something (e.g. `df_no_2 = df_no_2.reset_index(drop=True)`), like you do for most pandas operations. – DSM Jan 22 '15 at 12:36
  • Wow, till now I was dropping the Index column that appears with reset_index. Didn't know drop=True would take care of it. Wow. – fixxxer Apr 23 '15 at 18:10