题解 | #筛选某店铺最有价值用户中消费最多前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))

全部评论

相关推荐

听说改名字就能收到offer哈:Radis写错了兄弟
点赞 评论 收藏
分享
三年之期已到我的offer快到碗里来:9硕都比不上9本
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务