Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dropping a row in pandas with dates indexes, python

Tags:

python

pandas

I'm trying to drop the last row in a dataframe created by pandas in python and seem to be having trouble.

index = DateRange('1/1/2000', periods=8)
df = DataFrame(randn(8, 3), index=index, columns=['A', 'B', 'C'])

I tried the drop method like this:

df.drop([shape(df)[0]-1], axis = 0)

but it keeps saying label not contained in the axis.

I also tried to drop by index name and it still doesn't seem to be working.

Any advice would be appreciated. Thanks!!!

like image 374
user1504276 Avatar asked Jul 10 '12 13:07

user1504276


2 Answers

df.ix[:-1]

returns the original DataFrame with the last row removed.

like image 198
eumiro Avatar answered Oct 06 '22 00:10

eumiro


Referencing the DataFrame directly to retrieve all but the last index worked for me.

df[:-1]
like image 36
modpy Avatar answered Oct 05 '22 23:10

modpy