题解 | #某店铺用户消费特征评分#
某店铺用户消费特征评分
https://www.nowcoder.com/practice/200c824e9ed4428491c27d65ec56067d
import pandas as pd
#答案都是一样的死活不通过哭死……有看到的大哥留言下吗
fm1=pd.read_csv('sales.csv',sep=',')
fm=fm1.copy()
pd.set_option('display.max_columns',None)
pd.set_option('display.max_rows',None)
pd.set_option('display.width',300)
fm['R_Quantile']=None
R_Quantile_4=fm.recency<=fm.recency.quantile(0.25)
R_Quantile_3=(fm.recency>fm.recency.quantile(0.25))&(fm.recency<=fm.recency.quantile(0.5))
R_Quantile_2=(fm.recency>fm.recency.quantile(0.5))&(fm.recency<=fm.recency.quantile(0.75))
R_Quantile_1=fm.recency>fm.recency.quantile(0.75)
fm.loc[R_Quantile_4,'R_Quantile']=4
fm.loc[R_Quantile_3,'R_Quantile']=3
fm.loc[R_Quantile_2,'R_Quantile']=2
fm.loc[R_Quantile_1,'R_Quantile']=1
fm['F_Quantile']=None
F_Quantile_1=fm.frequency<=fm.frequency.quantile(0.25)
F_Quantile_2=(fm.frequency>fm.frequency.quantile(0.25))&(fm.frequency<=fm.frequency.quantile(0.5))
F_Quantile_3=(fm.frequency>fm.frequency.quantile(0.5))&(fm.frequency<=fm.frequency.quantile(0.75))
F_Quantile_4=fm.frequency>fm.frequency.quantile(0.75)
fm.loc[F_Quantile_4,'F_Quantile']=4
fm.loc[F_Quantile_3,'F_Quantile']=3
fm.loc[F_Quantile_2,'F_Quantile']=2
fm.loc[F_Quantile_1,'F_Quantile']=1
fm['M_Quantile']=None
M_Quantile_1=fm.monetary<=fm.monetary.quantile(0.25)
M_Quantile_2=(fm.monetary>fm.monetary.quantile(0.25))&(fm.monetary<=fm.monetary.quantile(0.5))
M_Quantile_3=(fm.monetary>fm.monetary.quantile(0.5))&(fm.monetary<=fm.monetary.quantile(0.75))
M_Quantile_4=fm.monetary>fm.monetary.quantile(0.75)
fm.loc[M_Quantile_4,'M_Quantile']=4
fm.loc[M_Quantile_3,'M_Quantile']=3
fm.loc[M_Quantile_2,'M_Quantile']=2
fm.loc[M_Quantile_1,'M_Quantile']=1
fm['R_Quantile']=fm['R_Quantile'].astype("int")
fm['F_Quantile']=fm['F_Quantile'].astype("int")
fm['M_Quantile']=fm['M_Quantile'].astype("int")
print(fm.head())