name = 'Merchant_id' # 折扣率排序 datas[name_prifix + 'discount_rateRank'] =datas.groupby(name)['discount_rate'].rank(ascending = False) datas[name_prifix + 'discount_rateRankAscend'] = datas.groupby(name)['discount_rate'].rank(ascending = True) # 距离排序 datas[name_prifix + 'distanceRank'] = datas.groupby(name)['Distance'].rank(ascending = False) datas[name_prifix + 'distanceRankAscend']=datas.groupby(name)['Distance'].rank(ascending = True) # 领券日期排序 datas[name_prifix + 'date_receivedRank'] =datas.groupby(name)['Date_received'].rank(ascending = False) datas[name_prifix + 'date_receivedRankAscend'] = datas.groupby(name)['Date_received'].rank(ascending = True)请帮我用groupby改写
时间: 2024-02-04 09:04:22 浏览: 86
com.xunmeng.merchant_Release_1ab0d9ae_ARM64.apk
好的,我可以帮你用groupby重写这段代码。你可以这样写:
```
cols = ['discount_rate', 'Distance', 'Date_received']
for col in cols:
datas[name_prifix + col + 'Rank'] = datas.groupby(name)[col].rank(ascending=False)
datas[name_prifix + col + 'RankAscend'] = datas.groupby(name)[col].rank(ascending=True)
```
这样,你就能用groupby对每一列进行操作,避免了多次重复写代码的问题。
阅读全文