Reputation: 404
This is a column in my dataframe df:
Price Peak
10 False
15 True
12 False
13 False
12 False
11 False
18 True
13 False
12 False
20 True
I want to count the waiting times between Trues. Desired output in this example would be:
[1, 4, 2]
How would I achieve this most efficiently? Thanks!
Upvotes: 0
Views: 42
Reputation: 32
This should do it /sw
peak = [False,True,False,False,False,False,True,False,False,True]
out_list = []
iCounter = 0
for _i in range(len(peak)):
if peak[_i] == True:
out_list.append(iCounter)
iCounter = 0
else:
iCounter += 1
print(out_list)
Upvotes: 1
Reputation: 64328
You can use itertools.groupby()
.
[
len(list(b_iter))
for b, b_iter in itertools.groupby(a)
if not b
]
Where len(list(b_iter))
is just one way to get the length of an iterator. There are others, e.g. sum(1 for x in b_iter)
Upvotes: 1
Reputation: 2802
You could probably get the output by using something similar to -
count = 0
output = []
For x in df.peak:
if x:
output.append(count)
count = 0
else:
count +=1
Basically you iterate over the column and use a counter to keep track of values. at the end of it all, output will be the array that your are looking for
Upvotes: 1