题解 | #筛选某店铺最有价值用户中消费最多前5名#
筛选某店铺最有价值用户中消费最多前5名
https://www.nowcoder.com/practice/58655010a7c34e9fb2b7b491c3f79ca4
import pandas as pd #读取数据 a=pd.read_csv(r'sales.csv') #准备好四分位数 r1=a['recency'].quantile(0.25) r2=a['recency'].quantile(0.5) r3=a['recency'].quantile(0.75) f1=a['frequency'].quantile(0.25) f2=a['frequency'].quantile(0.5) f3=a['frequency'].quantile(0.75) m1=a['monetary'].quantile(0.25) m2=a['monetary'].quantile(0.5) m3=a['monetary'].quantile(0.75) #给用户赋分 a.loc[a['recency']<=r1,'R_Quartile']=4 a.loc[(a['recency']>r1) & (a['recency']<=r2),'R_Quartile']=3 a.loc[(a['recency']>r2) & (a['recency']<=r3),'R_Quartile']=2 a.loc[a['recency']>r3,'R_Quartile']=1 a.loc[a['frequency']<=f1,'F_Quartile']=1 a.loc[(a['frequency']<=f2) & (a['frequency']>f1),'F_Quartile']=2 a.loc[(a['frequency']<=f3) & (a['frequency']>f2),'F_Quartile']=3 a.loc[a['frequency']>f3,'F_Quartile']=4 a.loc[a['monetary']<m1,'M_Quartile']=1 a.loc[(a['monetary']<=f2) & (a['monetary']>f1),'M_Quartile']=2 a.loc[(a['monetary']<=f3) & (a['monetary']>f2),'M_Quartile']=3 a.loc[a['monetary']>f3,'M_Quartile']=4 #由于class类需要合并 转为str而不用数字,否则加到一起了 a['R_Quartile']=a['R_Quartile'].astype(int).astype(str)#先转int类,去掉保留的一位小数,再转str a['F_Quartile']=a['F_Quartile'].astype(int).astype(str) a['M_Quartile']=a['M_Quartile'].astype(int).astype(str) #创建RFMClass列并整理数据 a['RFMClass']=a['R_Quartile']+a['F_Quartile']+a['M_Quartile'] a.drop(columns=['R_Quartile','F_Quartile','M_Quartile'],inplace=True) b=a.sort_values(by='RFMClass',ascending=False).reset_index(drop=True) print(a.head(),'\n',b.head())#小白#