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