Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get index of row by value in column

Tags:

python

pandas

I have the following dataframe:

     date         value
0    2016-01-01   gfhgh
1    2016-01-02   acgb
2    2016-01-03   yjhgs

I need to get the index of a row where date is a predefined value. For example for 2016-01-02, I need to get 1. Each date will be unique.

like image 335
darkpool Avatar asked Mar 14 '23 15:03

darkpool


2 Answers

Assuming the date field is string:

df[df.date == '<the date value whose index you want>'].index.tolist()

Will return a list of indices whose date is equal to the date value you provided

like image 107
Kaustav Datta Avatar answered Mar 16 '23 03:03

Kaustav Datta


IIUC you can use:

print df
        date  value
0 2016-01-01  gfhgh
1 2016-01-02   acgb
2 2016-01-03  yjhgs

print df[df['date'] == pd.to_datetime('2016-01-02')].index.tolist()
[1]
like image 25
jezrael Avatar answered Mar 16 '23 05:03

jezrael