什么不起作用:
def ATVScore(x,p,d):
if x <= d[p][0.25]:
return 4
elif x <= d[p][0.50]:
return 3
elif x <= d[p][0.75]:
return 2
else:
return 1
df_segmented['atv_quartile'] = df_segmented['Average_Transaction_Value'].apply(ATVScore, args = ('Average_Transaction_Value', quantiles,))
什么工作:
分位数定义:
quantiles = df_final_table.quantile(q=[0.25,0.5,0.75])
quantiles = quantiles.to_dict()
def RScore(x,p,d):
if x <= d[p][0.25]:
return 1
elif x <= d[p][0.50]:
return 2
elif x <= d[p][0.75]:
return 3
else:
return 4
def FMScore(x,p,d):
if x <= d[p][0.25]:
return 4
elif x <= d[p][0.50]:
return 3
elif x <= d[p][0.75]:
return 2
else:
return 1
df_segmented['recency_quartile'] = df_segmented['recency'].apply(RScore, args = ('recency', quantiles,))
df_segmented['frequency_quartile'] = df_segmented['frequency'].apply(FMScore, args = ('frequency', quantiles,))
df_segmented['monetary_quartile'] = df_segmented['monetary'].apply(FMScore, args = ('monetary', quantiles,))
数据:

错误:
