Counting a sequence of values ​​at the tail of a data frame

I am trying to get a 1s score in the tail of a pandas frame. From this data frame:

x 1 2 3 1 1 1 

I want to get a score of 3 . If there is no 1s in the tail, the function should return 0. I could not find any pandas function for this. Any ideas?

+1
source share
1 answer

Use this:

 d1.x[::-1].eq(1).cumprod().sum() 
+5
source

Source: https://habr.com/ru/post/1270113/


All Articles