题解 | #筛选某店铺最有价值用户中消费最多前5名#
筛选某店铺最有价值用户中消费最多前5名
https://www.nowcoder.com/practice/58655010a7c34e9fb2b7b491c3f79ca4
import pandas as pd df= pd.read_csv('sales.csv',delimiter=',') df['R_Quartile'] = pd.qcut(df['recency'],4,labels=[4,3,2,1]).astype('str') df['F_Quartile'] = pd.qcut(df['frequency'],4,labels=[1,2,3,4]).astype('str') df['M_Quartile'] = pd.qcut(df['monetary'],4,labels=[1,2,3,4]).astype('str') df['RFMClass'] = df['R_Quartile']+df['F_Quartile']+df['M_Quartile'] df = df.drop(columns=['R_Quartile','F_Quartile','M_Quartile']) # 设置pandas显示配置,以显示完整的数据框 pd.set_option("display.width", 300) pd.set_option("display.max_rows", None) pd.set_option("display.max_columns", None) print(df[:5]) # 写到这就算过了,后面不检查的 df.sort_values(by='RFMClass',ascending=False,inplace=True) print(df[:5])