1

I'm trying to use dataframe to select events. Let's say I have only two kinds of events: 1, -1 and the following dataframe:

a=pd.DataFrame({'ev':[1,1,-1,1,1,1,-1,-1]})

with:

a[a['ev']>0]

I can select only the 1 events. Now I would like to count how many consecutive events I have; in this case I would like to get [2,3]

The point is: how to get the list (or the series) of the indexes involved in a[a['ev']>0]?

user2988577
  • 3,997
  • 7
  • 21
  • 21

1 Answers1

-1

add .index to return a list of the indexes

In [43]: a=pd.DataFrame({'ev':[1,1,-1,1,1,1,-1,-1]})

In [44]: b = list(a[a['ev']>0].index)

In [45]: b
Out[45]: [0, 1, 3, 4, 5]
scott
  • 2,235
  • 1
  • 14
  • 18
  • Your result, `[0, 1, 3, 4, 5]`, is not the result sought - `[2, 3]`. – Yuval Langer Mar 08 '16 at 14:34
  • 1
    The result sought doesn't make any sense.. given his condition, the postions 2 and 3 correspond with a -1 and 1 respectively, and therefore the condition is not met by his result... I am showing him how to get a list of the indexes based on a condition, he can edit the condition to his heart's desire – scott Mar 08 '16 at 16:22
  • This answer is misleading given the title. It relies on the fact that values are only -1, 1 (which I see is in the question). Either this answer needs to change to match the title, or the title needs to change to say consecutive 1's or -1's. – cgnorthcutt Sep 04 '18 at 23:53
  • The last line of the question is: The point is: how to get the list (or the series) of the indexes involved in a[a['ev']>0]? - the answer does this. @cgnorthcutt – scott Oct 08 '18 at 15:38
  • @scott - the title says events but it should say '-1' and '1' as the solutions here don't work for other types of events like 3 and 4 for example. The title is more broad then the content. – cgnorthcutt Oct 09 '18 at 16:09
  • @cgnorthcutt sure - but since the answer is accepted and it's 4 years old - i'm not going to change it. people can read the content of the question, you can submit another answer for just the title if you like – scott Oct 09 '18 at 20:38