我有一列准时和一列包含二进制数据的列。每次二进制数从 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 个周期。
谢谢你!!
我有一列准时和一列包含二进制数据的列。每次二进制数从 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 个周期。
谢谢你!!
假设你的时间是等距的,所以 1.5s 总是例如 3 行,你可以很容易地循环完成:
for x in range(0, len(df), 3): #df being your dataframe
total = df.loc[x:x+3, 'Binary_data'].sum()
否则(不等距离),您需要:
df.groupby(pd.Grouper(freq='1.5s')).sum()
,pd
作为 pandas每经过一个循环,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="")