繁体   English   中英

计算同一列中连续行之间的差异

[英]Calculate difference between consecutive rows within a same column

我正在尝试计算“ Timestamp列中连续行之间的差异。 根据我的逻辑,我得到以下错误:

我的日志变量中包含如下数据:

['Timestamp:', '1546626931.138813', 'ID:', '0764', 'S', 'DLC:', '8', 00', '00', '00', '00', '00', '00', '00', '00', 'Channel:', '0']
['Timestamp:', '1546626931.138954', 'ID:', '0365', 'S', 'DLC:', '8', 00', '00', '00', '80', 'db', '80', 'a2', '7f', 'Channel:', '1']
['Timestamp:', '1546626931.139053', 'ID:', '0765', 'S', 'DLC:', '8', '0d', '0f', '00', '00', 'fd', '0e', '00', '01', 'Channel:', '1']
['Timestamp:', '1546626931.139289', 'ID:', '0766', 'S', 'DLC:', '8', 'fd', '0e', '02', '01', 'fc', '0e', '03', '01', 'Channel:', '1']
.
.
.
.

代码是:

import can
import csv
import datetime
import pandas


filename = open('C:\\Users\\xyz\\Downloads\\BLF File\\output.csv', "w")
log = can.BLFReader('C:\\Users\\xyz\\Downloads\\BLF File\\test.blf')

log_output = []
timestamp = []                        #Variable to store timestamps from blf file
time_del = []                         #Variable to store time difference
print('We are here 1')
for time in log:
    time = str(time).split()
    timestamp.append(float(time[1]))
    # print(timestamp)

print("we are here 2")
count = 0

for i in range(len(timestamp)-1):
    delta_float= timestamp[count+1] - timestamp[count]
    count = count + 1
print(delta_float)

我得到以下输出:

We are here 1
we ar here 2
0.00022101402282714844
0.0002288818359375
0.00021910667419433594
0.00024199485778808594
.
.
.
.

为什么我在delta_float中没有得到正确的区别? 根据日志变量中的值,我应该得到类似下面的内容,对吗?

0.141
0.99
0.236
.
.

为什么这种逻辑没有给我同一列Timestamp连续行之间的差异?

您只打印一个值,因为您只有一个print语句(不计算“我们在这里”的语句),它不在循环主体中,而是在打印标量值。 您必须至少更改其中的一项,以使其能够打印多个值,而第二项可能要做您想做的事情。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM