Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas get the "index" label of a series given an index

Tags:

pandas

Ok, so this is confusing because of a lack of vocabulary.

Pandas series have an index and a value: so 'series[0]' contains (index,value).

How do I get the index (in my case it is a date), out of the series by indexing the series? This is really a very simple idea...it is just encrypted by the word "index." lol.

So, to rephrase,

I need the date of the first entry in my series and the last entry, when my series is indexed by date.

just to be clear, I have a series indexed by date, so when I print it out, it prints:

12-12-2008 1.2
12-13-2008 1.3
...

and calling

df.ix[0] -> 1.2

I need:

df.something[0] -> 12-12-2008
like image 258
Chris Avatar asked Nov 26 '15 02:11

Chris


People also ask

How do I get the Pandas Series index name?

Use df. index. rename('foo', inplace=True) to set the index name. Seems this api is available since pandas 0.13.

How do you get the index of an element in a DataFrame in Pandas?

The get_loc() function is used to find the index of any column in the Python pandas dataframe. We simply pass the column name to get_loc() function to find index.


2 Answers

Got it.

 df.index[0]

yields the label at index 0.

like image 165
Chris Avatar answered Sep 18 '22 15:09

Chris


You can access the elements of your index just as you would a list. So df.index[0] will be the first element of your index and df.index[-1] will be the last.

Incidently if a series (or dataframe) has a non-integer index, df.ix[n] will return the n-th row corresponding to the n-th element of your index.

So df.ix[0] will return the first row and df.ix[-1] will return the last row. So an alternative way of getting the index values would be to use df.ix[0].name and df.ix[-1].name

like image 31
maxymoo Avatar answered Sep 18 '22 15:09

maxymoo