我如何用 pandas.groupby() 总结时间戳

How can i sum up timestamps with pandas.groupby()

我有一个日志(detection.csv)在脚本中检测到 class

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() 方法或任何其他方法

来总结检测到的 class 的持续时间

注意:两列均为字符串格式

当我使用 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)

是的,可以通过 to_timedelta 将列转换为时间增量并聚合 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')