我有一个groupby数据帧,我想返回具有最高值计数的前3个组
例如,下面的数据帧预期输出表应该是group
20,30和33
我想显示一个原始数据集表,但是组函数没有正确显示,所以我上传了一个图像
amount cosine_group
cosine_group
0 952.5 0
4 3000.0 4
20 2000.0 20
2000.0 20
2000.0 20
27 2000.0 27
30 2100.0 30
2100.0 30
2100.0 30
33 1065.0 33
1065.0 33
1065.0 33
1065.0 33
预期产出:
amount cosine_group
cosine_group
20 2000.0 20
2000.0 20
2000.0 20
30 2100.0 30
2100.0 30
2100.0 30
33 1065.0 33
1065.0 33
1065.0 33
1065.0 33
这可能不太像Python,但肯定能完成工作
您可以使用^{} 选择3个最大的尺寸。使用
.isin()
匹配具有这些值的行。最后,使用.loc
返回原始数据帧中最大元素的行,如下所示:或使用:
相关问题 更多 >
编程相关推荐