python-3.x - 我想根据熊猫的现金价值找到前 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
解决方案
使用GroupBy.rank
withascending=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
推荐阅读
- java - -source 1.5 错误中不支持 java 1.7+
- vim - 为什么 ctrl-t 插入奇怪的文本而不是在 fzf/vim 中打开一个新选项卡?
- java - 包不存在错误,即使它已经在 Java NetBeans 中导入
- docker - `opensuse/tumbleweed` docker 中的 `lsb_release -a`
- hibernate - 在休眠中映射视图和表
- ios - 在表单或列表中打开 fullscreenCover 或工作表时,第一次调用时传递给工作表内容的对象为零
- android - 带有导航组件和导航抽屉的单活动应用程序
- java - Wi-Fi 建议实际上如何适用于 Android 11?
- powershell - 屏幕截图路径模式不适用于 Azure Pipeline 中的 powershell
- postgresql - ModuleNotFoundError:没有名为“psycopg2”的模块 - 数据流