上一篇我們把消費次數、消費金額、最近一次的購買的距離天數,的資料整理完成後,我們要來將上面的資料分別轉為5個級距。
收先我們須要找出消費次數、消費金額、最近一次的購買的距離天數的最大值,並將數值列印出來。如圖1
customer_recency_max = customer_summary['最近一次的下單天數'].max()
customer_Frequency_max = customer_summary['消費次數'].max()
customer_Sales_max = customer_summary['消費總金額'].max()
print("最大的下單天數:", customer_recency_max)
print("最大的消費次數:", customer_Frequency_max)
print("最大的消費金額:", customer_Sales_max)
r_bins = [0, 19, 38, 57, 76, 95]
r_labels = ['5', '4', '3', '2', '1']
customer_summary['R'] = pd.cut(customer_summary['最近一次的下單天數'], bins=r_bins, labels=r_labels)
f_bins = [0,1,2,3,4,5]
f_labels = ['1', '2', '3', '4', '5']
customer_summary['F'] = pd.cut(customer_summary['消費次數'], bins=f_bins, labels=f_labels)
m_bins = [0,500,1000,2000,3000,14055]
m_labels = ['1', '2', '3', '4', '5']
customer_summary['M'] = pd.cut(customer_summary['消費總金額'],bins=m_bins, labels=m_labels)
分類完成後再將R、F、M的值轉成數值
customer_summary['R'] = customer_summary['R'].astype(int)
customer_summary['F'] = customer_summary['F'].astype(int)
customer_summary['M'] = customer_summary['M'].astype(int)
再來就是將每個客戶的RFM分成下面8種類型的客戶
1.RFM分數-高高高-重要價值客戶
2.RFM分數-低高高-重要保持客戶
3.RFM分數-高低高-重要發展客戶
4.RFM分數-高低低-一般發展客戶(新客戶)
5.RFM分數-低低高-重要挽留客戶
6.RFM分數-高高低-一般價值客戶
7.RFM分數-低高低-一般保持客戶
8.RFM分數-低低低-一般挽留客戶
根據上面的分數來寫一個顧客分類函數,這邊我是用第3級作為判斷的依據,
大於3就是屬於高,低於3就是屬於低
舉例:'R'、'F'、'M'欄的分數都大於3就歸類為【重要價值客戶】,以此類推
最後再新增一欄顧客類別,將對應的顧客類別新增到相對應的客戶ID,並儲存成EXCEL
# 定義顧客分類函數
def classify_customer(row):
if row['R'] >= 3 and row['F'] >= 3 and row['M'] >= 3:
return '重要價值客戶'
elif row['R'] <= 3 and row['F'] >= 3 and row['M'] >= 3:
return '重要保持客戶'
elif row['R'] >= 3 and row['F'] <= 3 and row['M'] >= 3:
return '重要發展客戶'
elif row['R'] >= 3 and row['F'] <= 3 and row['M'] <= 3:
return '一般發展客戶(新客戶)'
elif row['R'] <= 3 and row['F'] <= 3 and row['M'] >= 3:
return '重要挽留客戶'
elif row['R'] >= 3 and row['F'] >= 3 and row['M'] <= 3:
return '一般價值客戶'
elif row['R'] <= 3 and row['F'] >= 3 and row['M'] <= 3:
return '一般保持客戶'
elif row['R'] <= 3 and row['F'] <= 3 and row['M'] <= 3:
return '一般挽留客戶'
else:
return '其他'
# 新增一列 '顧客類別'
customer_summary['顧客類別'] = customer_summary.apply(classify_customer, axis=1)
# 將計算結果寫入 Excel 檔案
customer_summary.to_excel('RFM分析.xlsx', index=False)
下面為EXCEL的顯示畫面
下一篇會教如何將消費次數、消費金額、最近一次的購買的距離天數,做成圖表