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 14:04:22 浏览: 36
好的,我可以帮你用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对每一列进行操作,避免了多次重复写代码的问题。
相关问题
SELECT tcy.type_name, 60 AS merchantId, tcs.classify_big_id, GROUP_CONCAT(DISTINCT tcs.big_sort) AS bigSort FROM tb_commodity_sort tcs LEFT JOIN tb_commodity_classify tcy ON tcy.id = tcs.classify_big_id WHERE tcs.merchant_id = 60 GROUP BY tcs.classify_big_id ORDER BY bigSort DESC 排序会失效
这个 SQL 查询语句中的 `ORDER BY bigSort DESC` 语句并不会失效,它会按照 `bigSort` 字段的降序排列结果集。但需要注意的是,在使用 `GROUP_CONCAT()` 函数进行分组聚合时,得到的 `bigSort` 字段是一个字符串类型的值,而不是一个整数类型的值。因此,排序结果会按照字符串的字典序进行排序,而不是按照数字大小进行排序。如果要按照数字大小进行排序,可以使用 `CAST()` 函数将字符串类型的 `bigSort` 转换为整数类型,例如:
```
SELECT tcy.type_name,
60 AS merchantId,
tcs.classify_big_id,
GROUP_CONCAT(DISTINCT tcs.big_sort ORDER BY CAST(tcs.big_sort AS UNSIGNED) DESC) AS bigSort
FROM tb_commodity_sort tcs
LEFT JOIN tb_commodity_classify tcy ON tcy.id = tcs.classify_big_id
WHERE tcs.merchant_id = 60
GROUP BY tcs.classify_big_id
ORDER BY CAST(bigSort AS UNSIGNED) DESC
```
这样就可以按照数字大小进行降序排序了。
keys1 = ['User_id'] prefixs = 'history_' + '_'.join(keys1) + '_' # 用户核销率(领券消费数/领券数) feature_user[prefixs+'received_consume_rate'] = feature_user.apply(lambda x: x[prefixs+'received_consume_cnt']/x[prefixs+'received_cnt'] if x[prefixs+'received_cnt'] != 0 else 0, axis=1) # 用户在多少不同商家领取优惠券 pivot = pd.pivot_table(data[data['Date_received'].notnull()][['User_id', 'Merchant_id']], index=keys1, values='Merchant_id', aggfunc=lambda x:len(set(x))) pivot = pd.DataFrame(pivot).rename(columns={'Merchant_id':prefixs + 'received_differ_merchant'}).reset_index() feature_user = pd.merge(feature_user, pivot, on=keys1, how='left')改写代码 功能不变
keys1 = ['User_id']
prefixs = 'history_' + '_'.join(keys1) + '_'
# 用户核销率(领券消费数/领券数)
feature_user[prefixs + 'received_consume_rate'] = feature_user[prefixs + 'received_consume_cnt'] / feature_user[prefixs + 'received_cnt'].apply(lambda x: x if x != 0 else 1)
# 用户在多少不同商家领取优惠券
pivot = data[data['Date_received'].notnull()][['User_id', 'Merchant_id']].groupby(keys1)['Merchant_id'].nunique().reset_index()
pivot = pivot.rename(columns={'Merchant_id':prefixs + 'received_differ_merchant'})
feature_user = pd.merge(feature_user, pivot, on=keys1, how='left')
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)