# 用户在多少商家领取并消费优惠券 pivot = pd.pivot_table(data[data['Date'].notnull()&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_consume_differ_merchant'}).reset_index() feature_user = pd.merge(feature_user, pivot, on=keys1, how='left') # 用户对不同商家领取优惠券的核销率(领券消费数/领券数) feature_user[prefixs+'received_consume_merchant_rate'] = feature_user[prefixs + 'received_consume_differ_merchant'].astype('float')/feature_user[prefixs + 'received_differ_merchant'].astype('float') # 用户15天内消费优惠券数量 pivot = pd.pivot_table(data[data['label']==1][['User_id', 'cnt']], index=keys1, values='cnt', aggfunc=len) pivot = pd.DataFrame(pivot).rename(columns={'cnt': prefixs+'15_consume_cnt'}).reset_index() feature_user = pd.merge(feature_user, pivot, on=keys1, how='left') # 用户15天内未消费优惠券数量 pivot = pd.pivot_table(data[data['label']==0][['User_id', 'cnt']], index=keys1, values='cnt', aggfunc=len) pivot = pd.DataFrame(pivot).rename(columns={'cnt': prefixs+'15_not_consume_cnt'}).reset_index() feature_user = pd.merge(feature_user, pivot, on=keys1, how='left')用groupby(函数改写)
时间: 2024-01-21 19:05:03 浏览: 77
# 用户在多少商家领取并消费优惠券
pivot = data[data['Date'].notnull() & data['Date_received'].notnull()][['User_id', 'Merchant_id']].groupby(keys1)['Merchant_id'].nunique().reset_index()
pivot = pivot.rename(columns={'Merchant_id': prefixs + 'received_consume_differ_merchant'})
feature_user = pd.merge(feature_user, pivot, on=keys1, how='left')
# 用户对不同商家领取优惠券的核销率(领券消费数/领券数)
feature_user[prefixs + 'received_consume_merchant_rate'] = feature_user[prefixs + 'received_consume_differ_merchant'].astype('float') / feature_user[prefixs + 'received_differ_merchant'].astype('float')
# 用户15天内消费优惠券数量
pivot = data[data['label'] == 1][['User_id', 'cnt']].groupby(keys1)['cnt'].count().reset_index()
pivot = pivot.rename(columns={'cnt': prefixs+'15_consume_cnt'})
feature_user = pd.merge(feature_user, pivot, on=keys1, how='left')
# 用户15天内未消费优惠券数量
pivot = data[data['label'] == 0][['User_id', 'cnt']].groupby(keys1)['cnt'].count().reset_index()
pivot = pivot.rename(columns={'cnt': prefixs+'15_not_consume_cnt'})
feature_user = pd.merge(feature_user, pivot, on=keys1, how='left')