Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access last index value of dataframe

Tags:

python

pandas

I have an example dataframe (df) for 2 products:

                         BBG.XAMS.FUR.S               BBG.XAMS.MT.S
date                                                               
2014-10-23                 -2368.850388                    0.000000
2014-10-24                  6043.456178                    0.000000
2015-03-23                    -0.674996                   -0.674997
2015-03-24                    82.704951                   11.868748
2015-03-25                   -11.027327                   84.160210

Is there a way to retrieve the last index value of a dataframe only. So in this example the date value I need retrieved is 2015-03-25?

like image 893
Stacey Avatar asked Sep 01 '15 22:09

Stacey


People also ask

How do you find last index value?

lastIndexOf() The lastIndexOf() method returns the last index at which a given element can be found in the array, or -1 if it is not present.

How do I get the last element in pandas?

Python3. Pandas iloc is used to retrieve data by specifying its integer index. In python negative index starts from end therefore we can access the last element by specifying index to -1 instead of length-1 which will yield the same result. Pandas iat is used to access data of a passed location.


1 Answers

The following gives you the last index value:

df.index[-1]

Example:

In [37]:

df.index[-1]
Out[37]:
Timestamp('2015-03-25 00:00:00')

Or you could access the index attribute of the tail:

In [40]:

df.tail(1).index[0]
Out[40]:
Timestamp('2015-03-25 00:00:00')
like image 109
EdChum Avatar answered Oct 21 '22 18:10

EdChum