题解 | #筛选某店铺最有价值用户中消费最多前5名#
筛选某店铺最有价值用户中消费最多前5名
https://www.nowcoder.com/practice/58655010a7c34e9fb2b7b491c3f79ca4
import pandas as pd df1 = pd.read_csv('sales.csv',sep=',') df1['R_Quartile'] = pd.qcut(df1['recency'],[0,0.25,0.5,0.75,1],[4,3,2,1]).astype('str') df1["F_Quartile"] = pd.qcut(df1['frequency'],[0,0.25,0.5,0.75,1],[1,2,3,4]).astype('str') df1['M_Quartile'] = pd.qcut(df1['monetary'],[0,0.25,0.5,0.75,1],[1,2,3,4]).astype('str') df1['RFMClass'] = df1['R_Quartile'] + df1['F_Quartile'] + df1['M_Quartile'] print(df1.loc[:,['user_id','recency','frequency','monetary','RFMClass',]].head()) print(df1.loc[df1['RFMClass']=='444',['user_id','recency','frequency','monetary','RFMClass',]].sort_values(by="monetary",ascending=False).reset_index(drop=True).head())