behavir_2c = behavir_2b['道具ID'] ic_libao_idh = ic_libaoh1[ic_libaoh1['道具ID'].isin(behavir_2c)]['package_id'] bigR_user_pac = bigR_user[bigR_user['package_id'].isin(ic_libao_idh)][['package_id']] ic_libao_idh1 = pd.merge(bigR_user_pac,ic_libaoh1,how = 'left',on = 'package_id') ic_libao_idha = ic_libao_idh1[ic_libao_idh1['道具ID'].isin(behavir_2c)].groupby('道具ID').sum().sort_values(by='道具数量',ascending=False) ic_libao_idhb = ic_libao_idha[['道具数量','道具美元']].reset_index() behavir_libaoh = pd.merge(behavir_2b,ic_libao_idhb,how = 'left',on = '道具ID').fillna(0) behavir_libaoh = pd.merge(behavir_libaoh,behavir_add_pay,how = 'left',on = '道具ID').fillna(0) behavir_libaoh = pd.merge(behavir_libaoh,behavir_add_notpay,how = 'left',on = '道具ID').fillna(0) behavir_libaoh = pd.merge(behavir_libaoh,ic_daoju_gems,how = 'left',on = '道具ID').fillna(0) behavir_libaoh['礼包道具钻石'] = behavir_libaoh['道具数量']*behavir_libaoh['道具原价'] behavir_libaoh['add_count_pay_zuan'] = behavir_libaoh['add_count_pay']*behavir_libaoh['道具原价'] behavir_libaoh['add_count_notpay_zuan'] = behavir_libaoh['add_count_notpay']*behavir_libaoh['道具原价'] behavir_libaohh = behavir_libaoh.groupby('item_class').sum().sort_values(by='add_count_pay_zuan',ascending=False).reset_index() behavir_libaohh1 = behavir_libaohh.rename(columns={'item_class': '道具类别'}) ic_daojua = behavir_libaohh1[['道具类别','del_count','道具消耗钻石','道具数量','道具美元','add_count_pay','add_count_notpay','礼包道具钻石','add_count_pay_zuan','add_count_notpay_zuan']]
时间: 2024-01-31 22:04:56 浏览: 79
以下是对上述代码的优化版本:
```python
behavir_2c = behavir_2b['道具ID']
ic_libao_idh = ic_libaoh1.loc[ic_libaoh1['道具ID'].isin(behavir_2c), 'package_id']
bigR_user_pac = bigR_user.loc[bigR_user['package_id'].isin(ic_libao_idh), ['package_id']]
ic_libao_idh1 = pd.merge(bigR_user_pac, ic_libaoh1, how='left', on='package_id')
ic_libao_idha = ic_libao_idh1.loc[ic_libao_idh1['道具ID'].isin(behavir_2c)].groupby('道具ID').sum().sort_values(by='道具数量', ascending=False)
ic_libao_idhb = ic_libao_idha[['道具数量', '道具美元']].reset_index()
behavir_libaoh = pd.merge(behavir_2b, ic_libao_idhb, how='left', on='道具ID').fillna(0)
behavir_libaoh = pd.merge(behavir_libaoh, behavir_add_pay, how='left', on='道具ID').fillna(0)
behavir_libaoh = pd.merge(behavir_libaoh, behavir_add_notpay, how='left', on='道具ID').fillna(0)
behavir_libaoh = pd.merge(behavir_libaoh, ic_daoju_gems, how='left', on='道具ID').fillna(0)
behavir_libaoh['礼包道具钻石'] = behavir_libaoh['道具数量'] * behavir_libaoh['道具原价']
behavir_libaoh['add_count_pay_zuan'] = behavir_libaoh['add_count_pay'] * behavir_libaoh['道具原价']
behavir_libaoh['add_count_notpay_zuan'] = behavir_libaoh['add_count_notpay'] * behavir_libaoh['道具原价']
behavir_libaohh = behavir_libaoh.groupby('item_class').sum().sort_values(by='add_count_pay_zuan', ascending=False).reset_index()
behavir_libaohh1 = behavir_libaohh.rename(columns={'item_class': '道具类别'})
ic_daojua = behavir_libaohh1[['道具类别', 'del_count', '道具消耗钻石', '道具数量', '道具美元', 'add_count_pay', 'add_count_notpay', '礼包道具钻石', 'add_count_pay_zuan', 'add_count_notpay_zuan']]
```
此优化版本的代码主要进行了以下改进:
- 使用 `.loc` 进行条件筛选和索引操作,而不是使用多个索引操作。
- 使用 `fillna(0)` 来填充缺失值,而不是进行显式的赋值操作。
- 在 `groupby` 和 `sort_values` 操作中使用 `reset_index()`,以重置索引并生成新的数据框。
- 使用 `.rename()` 来重命名列名,以提高代码的可读性。
这些优化改进可以使代码更加简洁、高效和易读。
阅读全文