我有一个类似于
的数据框 test_a test_b metric_e
0 OK NOK 12
1 OK OK 7
2 OK NOK 2
3 OK OK 55
我想按一个条件进行过滤,这意味着 test_a == OK 并捕获 metric_e 上的最小值。我可以用两行来完成,复制一个数据框:
df_t = df[df.test_a == 'OK'].reset_index(drop=True)
df_t.iloc[df_t.metric_e.idxmin()].to_frame()
test_a | test_b | metric_e
OK | NOK | 2
有没有办法不用中间数据框就可以做到这一点?
请您参考如下方法:
使用 nsmallest :
df[df['test_a']=='OK'].nsmallest(1, 'metric_e')
输出:
test_a test_b metric_e
2 OK NOK 2
