Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding label location in a DataFrame Index

Tags:

I have a pandas dataframe:

import pandas as pnd d = pnd.Timestamp('2013-01-01 16:00') dates = pnd.bdate_range(start=d, end = d+pnd.DateOffset(days=10), normalize = False)  df = pnd.DataFrame(index=dates, columns=['a']) df['a'] = 6  print(df)                      a 2013-01-01 16:00:00  6 2013-01-02 16:00:00  6 2013-01-03 16:00:00  6 2013-01-04 16:00:00  6 2013-01-07 16:00:00  6 2013-01-08 16:00:00  6 2013-01-09 16:00:00  6 2013-01-10 16:00:00  6 2013-01-11 16:00:00  6 

I am interested in find the label location of one of the labels, say,

ds = pnd.Timestamp('2013-01-02 16:00') 

Looking at the index values, I know that is integer location of this label 1. How can get pandas to tell what the integer value of this label is?

like image 673
nitin Avatar asked Jun 21 '13 20:06

nitin


People also ask

How do you find the location of a value in a DataFrame?

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.

Which method is used for label location indexing by label?

loc attribute is the primary access method. The following are valid inputs: A single label, e.g. 5 or 'a', (note that 5 is interpreted as a label of the index. This use is not an integer position along the index)

What is an index label of a DataFrame?

Definition and Usage The index property returns the index information of the DataFrame. The index information contains the labels of the rows. If the rows has NOT named indexes, the index property returns a RangeIndex object with the start, stop, and step values.

How do I get DataFrame column labels?

To get the column names in Pandas dataframe you can type <code>print(df. columns)</code> given that your dataframe is named “df”.


1 Answers

You're looking for the index method get_loc:

In [11]: df.index.get_loc(ds) Out[11]: 1 
like image 175
Andy Hayden Avatar answered Sep 19 '22 05:09

Andy Hayden