2 回答

TA貢獻(xiàn)2003條經(jīng)驗(yàn) 獲得超2個(gè)贊
你可以做:
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
請(qǐng)注意,這不僅會(huì)給出計(jì)數(shù),還會(huì)基于transaction_count值給出交易的排名。

TA貢獻(xiàn)1936條經(jīng)驗(yàn) 獲得超7個(gè)贊
使用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
添加回答
舉報(bào)