我有以下Dataframe:
<表类>
指数
errorId
开始结束时间戳 uniqueId tbody><<tr>0 1404 2022-04-25 02:10:41 2022-04-25 02:10:46 2022-04-25 1404 _2022-04-25 11302 2022-04-25 02:10:41 2022-04-25 02:10:46 2022-04-25 1302 _2022-04-25 21404 2022-04-27 12:54:46 2022-04-27 12:54:51 2022-04-25 1404 _2022-04-25 3 1302 2022-04-27 13:34:43 2022-04-27 13:34:50 2022-04-25 1302 _2022-04-25 41404 2022-04-29 04:30:22 2022-04-29 04:30:29 2022-04-25 1404 _2022-04-25 51302 2022-04-29 08:26:25 2022-04-29 08:26:32 2022-04-25 1302 _2022-04-25 表类>
我认为你需要聚合min
和max
每3列命名聚合,最后为相同的列顺序,如原始添加DataFrame.reindex
:
df1 = (df.groupby(['errorId','timestamp','uniqueId'], as_index=False, sort=False)
.agg(start=('start','min'), end=('end','max'))
.reindex(df.columns, axis=1))
或按first
和last
聚合,如果日期时间按组排序,则得到相同的输出:
df2 = (df.groupby(['errorId','timestamp','uniqueId'], as_index=False, sort=False)
.agg(start=('start','first'), end=('end','last'))
.reindex(df.columns, axis=1))