python - Rank/Row Number Window Function in Python
问题描述
I am a newcomer to Python and I am needing a solution for this example below, This is how my df looks like,
I need to return just two records one for ID_Number 207921021 and one for ID_Number 222037001. Using the below code I was able to get one record for ID_Number 222037001 based on max value of Score 1 and Score 2 when the Action is the same ('Invest' in this case),
Data=Data.groupby(['ID_Number','Action'])['Score_1','Score_2'].max().reset_index()
Below was my result,
Is there way to just to get one record for ID_Number 207921021 as well (Two separate values in the action column this time i.e. 'Use' and 'Avoid')? I have used Window Functions in SQL i.e using QUALIFY RANK() OVER (PARTITION BY ID_number, Action ORDER BY Score_1,Score_2 DESC)=1
to get this done previously.
Is there a similar function in Python to do this?
解决方案
ID_Number
这将根据您定义的排序选择一行。
df.sort_values(by=['Score_2', 'Score_1'], ascending=[False, True]).groupby(['ID_Number']).head(1)
输出:
Action ID_Number Score_1 Score_2
3 Invest 222037001 9 0.4600
0 Use 207821021 7 0.4525
推荐阅读
- python - python中FFT的循环加速(使用`np.einsum`)
- android - 更改 UWP Xamarin 对话框上的主按钮样式
- kdb - 设置自动收报机时找不到 .u 变量
- angular - 未检测到指令
- regex - Python -- 从字符串中替换/删除 _XXX
- wso2 - WSO2 - 如何让调用中介忽略有效负载(比如它是空的,甚至不是空的)?
- python-3.x - geopandas中的groupby shapely类型
- javascript - 单击禁用按钮后
- java - 创建外观组件时将特定组件添加到 Spring Context
- python - 在 django 的一个会话变量中存储多个值