我如何用 pandas.groupby() 总结时间戳
Posted
技术标签:
【中文标题】我如何用 pandas.groupby() 总结时间戳【英文标题】:How can i sum up timestamps with pandas.groupby() 【发布时间】:2022-01-21 20:02:16 【问题描述】:我在脚本中有一个检测到的类的日志(detection.csv)
HP,0:00:08
Kellogs,0:02:03
Rayban,0:00:25
Skechers,0:00:09
Rayban,0:04:26
Skechers,0:02:34
HP,0:00:57
Rayban,0:00:14
HP,0:00:02
HP,0:00:08
Kellogs,0:02:06
Rayban,0:00:26
Skechers,0:00:10
问题是有没有办法使用 pandas.groupby() 方法或任何其他方法来总结检测到的类的持续时间
注意:两列都是字符串格式
当我使用 pandas.groupby() 方法时,结果没有汇总
OverallCode:
import numpy as np
import pandas as pd
csvdata=[]
with open('result2.txt','r+') as myfile:
for lines in myfile:
line=myfile.read()
line=line.replace(' ',',')
csvdata.append(line)
#print(csvdata)
with open('detection.csv','w') as newfile:
for i in range(len(csvdata)):
line=csvdata[i]
newfile.write(line)
newfile.close()
df=pd.read_csv('detection.csv',names=['class', 'timestamp'],header=None)
#ndf=df.groupby(['class'])['timestamp'].sum()
#print(ndf)
df['timestamp'] = pd.to_timedelta(df['timestamp'])
def format_timedelta(x):
ts = x.total_seconds()
hours, remainder = divmod(ts, 3600)
minutes, seconds = divmod(remainder, 60)
return ('::02d::02d').format(int(hours), int(minutes), int(seconds))
df1 = df.groupby('class')['timestamp'].sum().apply(format_timedelta).reset_index()
print (df1)
【问题讨论】:
【参考方案1】:是的,可以通过 to_timedelta
将列转换为 timedeltas 并聚合 sum
:
df['time'] = pd.to_timedelta(df['time'])
df1 = df.groupby('company', as_index=False)['time'].sum()
print (df1)
company time
0 HP 0 days 00:01:15
1 Kellogs 0 days 00:04:09
2 Rayban 0 days 00:05:31
3 Skechers 0 days 00:02:53
对于原始格式使用自定义函数:
df['time'] = pd.to_timedelta(df['time'])
def format_timedelta(x):
ts = x.total_seconds()
hours, remainder = divmod(ts, 3600)
minutes, seconds = divmod(remainder, 60)
return ('::02d::02d').format(int(hours), int(minutes), int(seconds))
df1 = df.groupby('company')['time'].sum().apply(format_timedelta).reset_index()
print (df1)
company time
0 HP 0:01:15
1 Kellogs 0:04:09
2 Rayban 0:05:31
3 Skechers 0:02:53
编辑:您可以简化代码:
csvdata=[]
with open('result2.txt','r+') as myfile:
for lines in myfile:
line=myfile.read()
line=line.replace(' ',',')
csvdata.append(line)
#print(csvdata)
with open('detection.csv','w') as newfile:
for i in range(len(csvdata)):
line=csvdata[i]
newfile.write(line)
newfile.close()
df=pd.read_csv('result2.csv',names=['class', 'timestamp'],header=None)
到:
#convert txt with tab separator
df=pd.read_csv('result2.txt',names=['class', 'timestamp'],header=None, sep='\t')
【讨论】:
结果不是我的总结 @NotSoFamous - 没有错误,没有求和?我很奇怪,因为我工作得很好。 @NotSoFamous - 选项卡分隔符有问题,您尝试使用df = pd.read_csv('detection.csv', sep='\t')
而不是df = pd.read_csv('detection.csv')
吗?
@ezrael 我已经使用上面的代码更新了问题仍然相同的结果你能告诉我我缺少什么
@jezarel Thnx 你为我节省了很多时间 :)以上是关于我如何用 pandas.groupby() 总结时间戳的主要内容,如果未能解决你的问题,请参考以下文章