# 优惠券15天内核销率 feature_Coupon[prefixs+'15_consume_rate'] = list(map(lambda x,y: x/y if y!=0 else 0, feature_Coupon[prefixs + '15days_consume_cnt'], feature_Coupon[prefixs + 'received_cnt'])) # 优惠券15天内被核销的最大时间间隔 t6 = data[data['label']==1].copy() t6['time_gap'] = (t6['date'] - t6['date_received']).map(lambda x:x.total_seconds()/(24*60*60)) pivot = pd.pivot_table(t6, index=keys4, values='time_gap', aggfunc=np.max) pivot = pd.DataFrame(pivot).rename(columns={'time_gap':prefixs + '15days_max_time_gap'}).reset_index() feature_Coupon = pd.merge(feature_Coupon, pivot, on=keys4, how='left')请用groupby(函数改写)
时间: 2024-03-22 18:37:02 浏览: 99
齐博cms_v7
可以使用groupby函数对数据进行分组并聚合,改写上述代码如下:
```
# 优惠券15天内核销率
feature_Coupon[prefixs+'15_consume_rate'] = feature_Coupon.apply(lambda x: x[prefixs + '15days_consume_cnt'] / x[prefixs + 'received_cnt'] if x[prefixs + 'received_cnt'] != 0 else 0, axis=1)
# 优惠券15天内被核销的最大时间间隔
t6 = data[data['label'] == 1].copy()
t6['time_gap'] = (t6['date'] - t6['date_received']).dt.days
pivot = t6.groupby(keys4)['time_gap'].max().reset_index()
pivot = pivot.rename(columns={'time_gap': prefixs + '15days_max_time_gap'})
feature_Coupon = pd.merge(feature_Coupon, pivot, on=keys4, how='left')
```
其中,对于优惠券15天内核销率的计算,可以使用apply函数对每一行数据进行计算。对于优惠券15天内被核销的最大时间间隔的计算,可以使用groupby函数对keys4进行分组,并对每组数据的time_gap列进行最大值的聚合。
阅读全文