【问题标题】:Subtraction between rows in a time column时间列中的行之间的减法
【发布时间】:2020-05-15 13:59:15
【问题描述】:

我有一个 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

【问题讨论】:

  • 发布示例数据和预期输出
  • 现在可以看到了

标签: python loops dataframe time


【解决方案1】:

试试这个:

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

输出:

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

【讨论】:

  • 它对数据框没有任何作用
  • 你检查过它在打印什么
  • 是的,它正在打印相同的输入,我也可以从尺寸中看到
  • 你能分享一下代码我会检查有没有问题
猜你喜欢
  • 2016-03-26
  • 1970-01-01
  • 2011-11-17
  • 1970-01-01
  • 2016-10-17
  • 2019-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多