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?
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.
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)
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.
To get the column names in Pandas dataframe you can type <code>print(df. columns)</code> given that your dataframe is named “df”.
You're looking for the index method get_loc
:
In [11]: df.index.get_loc(ds) Out[11]: 1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With