题解 | #某店铺用户消费特征评分#
某店铺用户消费特征评分
https://www.nowcoder.com/practice/200c824e9ed4428491c27d65ec56067d
import sys import pandas as pd # for line in sys.stdin: # a = line.split() # print(int(a[0]) + int(a[1])) data = pd.read_csv('sales.csv') recency = data['recency'] frequency = data['frequency'] monetary = data['monetary'] # 计算各列四分位数和中位数 xs1 = recency.quantile(0.25) #计算下四分位数 mid1 = recency.median() #计算中位数 ss1 = recency.quantile(0.75) #计算下四分位数 xs2 = frequency.quantile(0.25) mid2 = frequency.median() ss2 = frequency.quantile(0.75) xs3 = monetary.quantile(0.25) mid3 = monetary.median() ss3 = monetary.quantile(0.75) def re_score(value): #定义评分函数 if value <= xs1: return 4 elif value>xs1 and value<=mid1: return 3 elif value>mid1 and value<=ss1: return 2 else: return 1 data['R_Quartile'] = recency.apply(re_score) #创建评分列并添加到表后 def fre_score(value): if value <= xs1: return 1 elif value>xs1 and value<=mid1: return 2 elif value>mid1 and value<=ss1: return 3 else: return 4 data['F_Quartile'] = frequency.apply(fre_score) data['M_Quartile'] = monetary.apply(fre_score) print(data.head(5))