我有一个数据框df
像这样:
我想按 region
对其进行分组并返回 metrics
的最小值每个组中,以及 model
值其中 metrics
是最小值。
预期结果:
| region | model | metrics|
| -------- | --------- |----|
| Tokyo | ARIMA |0.1|
| Osaka | DeepAR |0.1|
我尝试像下面这样做,但不确定如何完成:
df.groupby("region").agg({'metrics':'min', ####... })
也许使用argmin
?任何帮助将不胜感激。谢谢!
最佳答案
您可以找到每个组的最小指标的索引,然后将其loc
到原始帧中:
# not sorting to keep the original order of appearance of regions
min_inds = df.groupby("region", sort=False).metrics.idxmin()
result = df.loc[min_inds]
获取
>>> result
region model metrics
0 Tokyo ARIMA 0.1
5 Osaka DeepAR 0.1
(愿预测很有趣!)
关于python - Pandas groupby 获取另一列最小的列的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68168208/