[英]Merge rows in dataframe based on Timestamp
I want to merge rows of data frame based on similar timestamp.我想根据类似的时间戳合并数据帧的行。
import pandas as pd
df = pd.DataFrame([VEST,False,0.6993550658226013,2019-11-27 18:56:12.616425+05:30],
[HELMET,True,0.8506404161453247 ,2019-11-27 18:56:12.616425+05:30],
[HELMET,True,0.5948962569236755 ,2019-11-27 18:56:13.617801+05:30],
[VEST,False,0.6576083898544312 ,2019-11-27 18:56:14.595118+05:30],
[HELMET,True,0.8451269865036011 ,2019-11-27 18:56:14.595118+05:30],
[VEST,True,0.7157155275344849 ,2019-11-27 18:56:15.625841+05:30],
[HELMET,True,0.80693519115448 ,2019-11-27 18:56:15.625841+05:30],
[HELMET,True,0.5428823232650757 ,2019-11-27 18:56:41.639505+05:30],
[VEST,False,0.6302998661994934 ,2019-11-27 18:56:42.582407+05:30],
[HELMET,True,0.8790003657341003 ,2019-11-27 18:56:42.582407+05:30],
[VEST,False,0.44062405824661255 ,2019-11-27 18:56:44.590130+05:30],
[HELMET,True,0.9355553388595581, 2019-11-27 18:56:44.590130+05:30 ],columns = ['Type', 'voilation', 'score', 'timestamp'])
Is there any way to merge rows with similar type and timestamp (2-3 secs) and assign violation type based on highest score.有没有办法合并具有相似类型和时间戳(2-3 秒)的行并根据最高分分配违规类型。
df.groupby(['Type', 'timestamp'])
Groupby generates only 3 frames. Groupby 仅生成 3 帧。 Not able to figure quite what to do.
无法弄清楚该怎么做。 Any help is appreciated.
任何帮助表示赞赏。
You can use pandas.Series.dt.round to round your timestamp to the nearest three seconds and then group,您可以使用pandas.Series.dt.round将时间戳舍入到最接近的三秒,然后分组,
df['rounded_timestamp'] = pd.to_datetime(df['timestamp']).dt.round('3s')
df1 = df.groupby(['Type', 'rounded_timestamp']).agg({'score': 'max'}).reset_index()
>>>df1
Type rounded_timestamp score
0 HELMET 2019-11-27 13:26:12 0.850640
1 HELMET 2019-11-27 13:26:15 0.845127
2 HELMET 2019-11-27 13:26:42 0.879000
3 HELMET 2019-11-27 13:26:45 0.935555
4 VEST 2019-11-27 13:26:12 0.699355
5 VEST 2019-11-27 13:26:15 0.715716
6 VEST 2019-11-27 13:26:42 0.630300
7 VEST 2019-11-27 13:26:45 0.440624
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.