2 回答

TA贡献2003条经验 获得超2个赞
你可以做:
df['rank'] = df.groupby('cust_ID')['transaction_count'].rank(ascending=False)
输出:
cust_ID associate_ID transaction_count rank
0 1234 608 4 1.0
1 1234 785 1 2.0
2 4789 345 2 1.0
3 3456 268 5 1.0
4 3456 725 3 2.0
5 3456 795 1 3.0
请注意,这不仅会给出计数,还会基于transaction_count值给出交易的排名。

TA贡献1936条经验 获得超6个赞
使用groupby + cumcount:
df['rank'] = df.groupby('cust_ID').cumcount() + 1
print(df['rank'])
输出
0 1
1 2
2 1
3 1
4 2
5 3
Name: rank, dtype: int64
添加回答
举报