gpt4 book ai didi

python - 如何选择 Pandas 中每组的前 3 行?

转载 作者:太空宇宙 更新时间:2023-11-03 12:55:00 27 4
gpt4 key购买 nike

我得到一个像这样的 pandas 数据框:

    id   prob
0 1 0.5
1 1 0.6
2 1 0.4
3 1 0.2
4 2 0.3
6 2 0.5
...

我想按“id”对其进行分组,降序排序并获得每组的前 3 个概率。请注意,某些组包含的行数少于 3。最后我想得到一个二维数组,如:

[[1, 0.6, 0.5, 0.4], [2, [0.5, 0.3]]...]

我怎样才能用 pandas 做到这一点?谢谢!

最佳答案

使用sort_valuesgroupbyhead:

df.sort_values(by=['id','prob'], ascending=[True,False]).groupby('id').head(3).values

输出:

array([[ 1. ,  0.6],
[ 1. , 0.5],
[ 1. , 0.4],
[ 2. , 0.5],
[ 2. , 0.3]])

跟随@COLDSPEED 领导:

df.sort_values(by=['id','prob'], ascending=[True,False])\
.groupby('id').agg(lambda x: x.head(3).tolist())\
.reset_index().values.tolist()

输出:

[[1, [0.6, 0.5, 0.4]], [2, [0.5, 0.3]]]

关于python - 如何选择 Pandas 中每组的前 3 行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45992871/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com