首页 > 解决方案 > 我想根据熊猫的现金价值找到前 3 名的客户

问题描述

对于客户,我需要根据现金价值找出排名值

输入

编号 概率
1 0.000000
1 0.000000
1 0.000000
1 0.000000
1 0.000000
1 0.000000
3 0.000000
3 0.000000
2748 0.43668
2354 0.43616

输出

Id 没有概率等级

1     0.000000      15
1     0.000000      15
1     0.000000      15
1     0.000000      15
1     0.000000      15
1     0.000000      15
3     0.000000      10
3     0.000000      10

2748 0.43668 1 2354 0.43616 1

标签: python-3.xpandasdataframe

解决方案


使用GroupBy.rankwithascending=False并转换为整数:

df['rank'] = df.groupby('Cust_id')['cash'].rank(ascending=False).astype(int)
print (df)
   Cust_id Things   cash  rank
0        1    bag  25000     1
1        1    tag   5000     2
2        1     bg   2000     3
3        2    pen   5000     2
4        2    pet   4000     3
5        2    red   7000     1

推荐阅读