PANDAS drop a range of rows from df

Question:

I want to drop m number of rows from the bottom of a data frame. It is integer indexed (with holes). How can this be done?

pandas == 0.10.1
python == 2.7.3

Asked By: beets

||

Answers:

This should work

df.head(len(df) - m)
Answered By: user1827356

Use slice to select the part you want:

df[:-m]

If you want to remove some middle rows, you can use drop:

df.drop(df.index[3:5])
Answered By: HYRY

an example where the range you want to drop is indexes between x and y which I have set to 0 and 10

selecting just the locations between 0 and 10 to see the rows to confirm before removing

x=0 # could change x and y to a start and end date
y=10 
df.loc[x:y]

selecting the index

df.loc[x:y].index

so to remove selection from dataframe

df.drop(df.loc[x:y].index, inplace=True)
Answered By: Julia Cowper
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.