-1

我有一列准时和一列包含二进制数据的列。每次二进制数从 1 变为 0,即为 1 个周期。我想将每 1.5 秒的周期数相加。

就像是

前 1.5 秒 - 1,0,1,0 -> 2 个周期

第二个 1.5s - 1,0 -> 1 个周期

第三个 1.5 秒 - 1,0,1,0,1,0 -> 3 个周期。

谢谢你!!

4

2 回答 2

0

假设你的时间是等距的,所以 1.5s 总是例如 3 行,你可以很容易地循环完成:

for x in range(0, len(df), 3): #df being your dataframe
    total = df.loc[x:x+3, 'Binary_data'].sum()

否则(不等距离),您需要:

  1. 将您的时间列转换为 datetime-dtype
  2. 使用 grouper: df.groupby(pd.Grouper(freq='1.5s')).sum()pd作为 pandas
于 2021-04-16T07:59:42.760 回答
0

每经过一个循环,cycles += 1在代码中添加:

from time import time, sleep
start = time()
cycles = 0
sleep(0.5)
def cycle_todo(): #Define cycle!
    sleep(0.05)
while True:
    cycle_todo()
    cycles += 1
    print("Average cycles / 1.5 s: {}     ".format(round(cycles/((time()-start)/1.5), 3)),end="")
    print("\r", end="")
于 2021-05-12T16:38:48.577 回答