③ 第三步
def functions(x): if x.iloc[0]==1 and x.iloc[1]==1 and x.iloc[2]==1: return "重要价值客户" elif x.iloc[0]==1 and x.iloc[1]==1 and x.iloc[2]==0: return "潜力客户" elif x.iloc[0]==1 and x.iloc[1]==0 and x.iloc[2]==1: return "重要深耕客户" elif x.iloc[0]==1 and x.iloc[1]==0 and x.iloc[2]==0: return "新客户" elif x.iloc[0]==0 and x.iloc[1]==1 and x.iloc[2]==1: return "重要唤回客户" elif x.iloc[0]==0 and x.iloc[1]==1 and x.iloc[2]==0: return "一般客户" elif x.iloc[0]==0 and x.iloc[1]==0 and x.iloc[2]==1: return "重要挽回客户" elif x.iloc[0]==0 and x.iloc[1]==0 and x.iloc[2]==0: return "流失客户" df2["标签"] = df2[["R-SCORE是否大于均值","F-SCORE是否大于均值","M-SCORE是否大于均值"]].apply(functions,axis=1) df2.sample(10)
结果如下:
4)可视化展示
① 绘制不同类型客户的人数对比
df3 = df2.groupby("标签").agg({"标签":"count"}) df3["不同客户的占比"] = df3["标签"].apply(lambda x:x/np.sum(df3["标签"])) df3 = df3.sort_values(by="标签",ascending=True) plt.figure(figsize=(6,4),dpi=100) x = df3.index y = df3["标签"] plt.barh(x,height=0.5,width=y,align="center") plt.title("不同类型客户的人数对比") for x,y in enumerate(y): plt.text(y+450,x,y,ha="center",va="center",fontsize=14) plt.xticks(np.arange(0,10001,2000)) plt.tight_layout() plt.savefig("不同类型客户的人数对比",dpi=300)
结果如下:
② 绘制不同类型客户人数占比图
df3 = df2.groupby("标签").agg({"标签":"count"}) df3["不同客户的占比"] = df3["标签"].apply(lambda x:x/np.sum(df3["标签"])) df3 = df3.sort_values(by="标签",ascending=True) plt.figure(figsize=(7,4),dpi=100) x = df3["不同客户的占比"] labels = ['潜力客户', '一般客户', '重要价值客户', '重要唤回客户', '重要深耕客户', '新客户', '重要挽回客户', '流失客户'] colors = ['#9999ff','#ff9999','#7777aa','#2442aa','#dd5555','deeppink','yellowgreen','lightskyblue'] explode = [0,0,0,0,0,0,0,0] patches,l_text = plt.pie(x,labels=labels,colors=colors, explode=explode,startangle=90,counterclock=False) for t in l_text: t.set_size(0) plt.axis("equal") plt.legend(loc=(0.001,0.001),frameon=False) plt.title("不同类型客户人数占比图") plt.savefig("不同类型客户人数占比图",dpi=300)
结果如下: