How to delete the last row of data of a pandas dataframe?

 I think this should be simple, but I tried a few ideas and none of them worked:

last_row = len(DF)
DF = DF.drop(DF.index[last_row])  #<-- fail!

I tried using negative indices but that also lead to errors. I must still be misunderstanding something basic. 

Please help how pandas drop last row? Thanks in advance.

Answered by Cameron Oliver

For dropping last n rows:

  df.drop(df.tail(n).index,inplace=True) # drop last n rows

Similarly, you can drop first n rows:

df.drop(df.head(n).index,inplace=True) # drop first n rows

Your Answer

Interviews

Parent Categories