python - 时间列中的行之间的减法
问题描述
我有一个 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
解决方案
尝试这个:
df = df.groupby('Time')['Length'].sum().reset_index()
print(df)
输出:
Time Length
0 11:53:59 15
1 11:54:00 30
推荐阅读
- c# - Asp .NET Core Identity Server 从登录重定向中排除一些 url
- ibm-certificate-manager - 使用 IBM Certificate Manager 中的证书和密钥时自签名证书的 TLS 错误
- python - 在 Dask 中使用 read_sql_table 将时间戳分配为索引
- google-apps-script - esp-32 将图像发送到 Gdrive
- email - Outlook 删除
? - python - RGBA 透明度在叠加到图像上时输出不清晰
- python - 如何使用 python 重命名和排序 .eml 文件中的标题?
- javascript - 如何在用户注销时清除 sessionStorage
- python - Selenium 无效参数:无效的“sameSite”
- apache-kafka - 在通过 CamelKafkaAzureBlobSink 连接器的每条记录上附加一个换行符