5

Given a Pandas dataframe, where one of the columns looks like this:

Date
2016-04-15
2016-04-14
2016-04-13
2016-04-12 
2016-04-11
2016-04-08

How do I get the row-index of a particular value assuming that values are unique?

For example, "2016-04-13" would return 2

0

3 Answers 3

6

With boolean indexing, you can slice the dataframe to get only the rows where the date equals "2016-04-13" and get the index of the slice:

df[df.Date == "2016-04-13"].index
Out[37]: Int64Index([2], dtype='int64')

With the uniqueness assumption, there will be only one element in that array, so you can take the 0th element:

df[df.Date == "2016-04-13"].index[0]
Out[38]: 2
Sign up to request clarification or add additional context in comments.

Comments

5

Use df.index.get_loc('2016-04-14') to get the integer location for requested label. This will return 1 as intital starts from 0. So you can add one to get the index value as 2

1 Comment

Not very Pythonic. @ayhan's is. Probably more performant too.
0

df['Date'].values.tolist().index("2016-04-13") will return 2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.