这是我的数据框df中的一列:
Price Peak
10 False
15 True
12 False
13 False
12 False
11 False
18 True
13 False
12 False
20 True
我想计算Trues之间的等待时间。在此示例中,所需的输出为:
[1、4、2]
我将如何最有效地实现这一目标?谢谢!
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)
count = 0
output = []
For x in df.peak:
if x:
output.append(count)
count = 0
else:
count +=1
基本上,您遍历该列并使用计数器来跟踪值。最后,输出将是您正在寻找的数组
itertools.groupby()
。