Home > front end >  Drop rows without reset index pandas
Drop rows without reset index pandas

Time:01-13

I have this dataframe

df = pd.DataFrame({'A': [1, 2, 3, 4, 5, 6, 7, 8, 9],
               'B': [9, 8, 7, 6, 5, 4, 3, 2, 1]})

with index RangeIndex(start=0, stop=9, step=1). I want to remove the first two rows, so

df=df.drop(df.index[:2])

If I print the index now it says RangeIndex(start=2, stop=9, step=1).

The problem is that if I call an element like df.iloc[0] it still gives me the first row and it shouldn't, because it has and index=2.

I have a big dataframe and I want to delete it step by step after some computations but I would like to set the index so that after each drop it doesn't reset and according with the example it gives me for example the first row if I ask df.iloc[2] and not df.iloc[0]

CodePudding user response:

I think you can use 'df.loc' instead of 'df.iloc'

  • Related