题解 | #筛选某店铺最有价值用户中消费最多前5名#
筛选某店铺最有价值用户中消费最多前5名
https://www.nowcoder.com/practice/58655010a7c34e9fb2b7b491c3f79ca4
import pandas as pd df=pd.read_csv('sales.csv',sep=',') df_des=df.describe() def score_r(number): if number<=df_des.loc['25%','recency']: return 4 elif number>df_des.loc['25%','recency'] and number<=df_des.loc['50%','recency']: return 3 elif number>df_des.loc['50%','recency'] and number<=df_des.loc['75%','recency']: return 2 else: return 1 def score_f(number): if number<=df_des.loc['25%','frequency']: return 1 elif number>df_des.loc['25%','frequency'] and number<=df_des.loc['50%','frequency']: return 2 elif number>df_des.loc['50%','frequency'] and number<=df_des.loc['75%','frequency']: return 3 else: return 4 def score_m(number): if number<=df_des.loc['25%','monetary']: return 1 elif number>df_des.loc['25%','monetary'] and number<=df_des.loc['50%','monetary']: return 2 elif number>df_des.loc['50%','monetary'] and number<=df_des.loc['75%','monetary']: return 3 else: return 4 df['R_Quartile']=df.recency.apply(lambda x :str(score_r(x))) df['F_Quartile']=df.recency.apply(lambda x :str(score_f(x))) df['M_Quartile']=df.recency.apply(lambda x :str(score_m(x))) df['RFMClass']=df['R_Quartile']+df['F_Quartile']+df['M_Quartile'] df=df[['user_id','recency','frequency','monetary','RFMClass']] print(df.head(5)) print(df[df['RFMClass']=='444'].reset_index(drop=True))