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')改写代码 功能不变
时间: 2024-01-21 07:05:03 浏览: 69
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')
相关问题
# 用户核销率(领券消费数/领券数) 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')改写代码,功能不变
# 用户核销率(领券消费数/领券数)
feature_user['received_consume_rate'] = feature_user['received_consume_cnt'] / feature_user['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': 'received_differ_merchant'})
feature_user = pd.merge(feature_user, pivot, on=keys1, how='left')
# 用户在多少商家领取并消费优惠券 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(函数改写)
# 用户在多少商家领取并消费优惠券
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')
阅读全文