首页 > 解决方案 > 时间列中的行之间的减法

问题描述

我有一个 csv 文件,我将其过滤为两列,其中一列具有时间戳值,另一列是数字在时间的第一列中的同一秒我在处理时间以及算法本身方面遇到了问题这是我进行的一项试验:

df=pd.DataFrame(data=Input_filtered) #This is a dataframe of the file with only the 2 columns

for i in range(0,Input_filtered_size,1):
        row1, row2 = df.iloc[i], df.iloc[i+1]
        if row2['Time'] - row1['Time'] <= 1: #Iam not sure if comparing the time difference with 1 is right or not 
            df.iloc[i] = (row2['Number of Samples'] + row1['Number of Samples'])

编辑:两列的样本

Time    Length
11:53:59    1
11:53:59    2
11:53:59    3
11:53:59    4
11:53:59    5
11:54:00    6
11:54:00    7
11:54:00    8
11:54:00    9    

等等我需要做的是以秒为单位对所有具有相同时间的样本求和,因此建议的输出将是:

Time    Length
11:53:59    15
11:54:00    30

标签: pythonloopsdataframetime

解决方案


尝试这个:

df = df.groupby('Time')['Length'].sum().reset_index()
print(df)

输出:

    Time    Length
0   11:53:59    15
1   11:54:00    30

推荐阅读