Reputation: 429
in, df there is a column called Signal. What I want to do is calculate consequent 2's and add that number to the List.
example:
lst=[1,7,8.....]
1 means 1 consequent 2's, then 7 consequent 2's and so on.
Data:
{'Signal': {0: 2,
1: 3,
2: 2,
3: 2,
4: 2,
5: 2,
6: 2,
7: 2,
8: 2,
9: 3,
10: 3,
11: 3,
12: 3,
13: 3,
14: 3,
15: 3,
16: 3,
17: 3,
18: 3,
19: 3,
20: 3,
21: 3,
22: 3,
23: 3,
24: 2,
25: 2,
26: 2,
27: 2,
28: 2,
29: 2,
30: 2,
31: 2,
32: 3,
33: 3,
34: 3,
35: 3,
36: 3,
37: 2,
38: 2,
39: 3,
40: 3,
41: 3,
42: 3,
43: 3,
44: 3,
45: 3,
46: 3,
47: 3,
48: 3,
49: 3}}
Upvotes: 0
Views: 53
Reputation: 862611
First compare by not equal 2
, create consecutive groups, filter only 2
by invert mask and call Series.value_counts
:
m = df['Signal'].ne(2)
lst = m.cumsum()[~m].value_counts().sort_index().tolist()
print (lst)
[1, 7, 8, 2]
Or use GroupBy.size
for count consecutive 2
:
m = df['Signal'].ne(2)
lst = df[~m].groupby(m.cumsum()).size().tolist()
Upvotes: 2