defrfm_func(x):
level = x.map(lambda x :'1'if x >=0else'0')
label = level.R + level.F + level.M
d ={'111':'重要价值客户','011':'重要保持客户','101':'重要挽留客户','001':'重要发展客户','110':'一般价值客户','010':'一般保持客户','100':'一般挽留客户','000':'一般发展客户'}
result = d[label]return result
rfm['label']= rfm[['R','F','M']].apply(lambda x : x -x.mean()).apply(rfm_func,axis =1)
rfm.head()
第五部分:用户生命周期
5.1统计每个用户每个月是否消费,消费为1反之为0
user_month_count_df = df.pivot_table(index='user_id',values='order_dt',aggfunc='count',columns='month').fillna(0)
df_purchase=user_month_count_df.applymap(lambda x:1if x >=1else0)defactive_status(data):
status =[]for i inrange(18):if data[i]==0:iflen(status)>0:if status[i-1]=='unreg':
status.append('unreg')else:
status.append('unactive')else:
status.append('unreg')else:iflen(status)==0:
status.append('new')else:if status[i-1]=='unactive':
status.append('return')if status[i-1]=='unreg':
status.append('new')else:
status.append('active')return status
pivoted_status = df_purchase.apply(active_status,axis =1)
pivoted_status.head()