不使用嵌套子句
,这是非常低效的,排序值
和删除副本(_D)
:
out=(df.sort_values(by=[“查询”,“持续时间”,“变量”]).drop_duplicates(['query','variant'],ignore_index=True))
输出:
查询变量持续时间0问题1 c 201问题1 b 302问题1 a 403问题2 a 90
或者,如果行的最终顺序无关紧要,分组.idxmin
:
out=df.loc[df.groupby(['query','variant'])['duration'].idxmin()]
输出:
查询变量持续时间1问题1 a 402季度1 b 303问题1 c 204问题2 a 90
为了完整起见,您需要添加一个水位下降
您的方法:
df.groupby(“查询”).apply(lambda x:x.groupby(“变体”).apply(λx:x.sort_values(“持续时间”).head(1),include_groups=False).sort_values(“持续时间”),include_groups=False).droplevel(-1)
输出:
期间查询变量问题1 c 20b 30岁a 40岁问题2 a 90