根据RFM分数细分客户并创建pandas DataFrame

问题描述 投票:0回答:1

我已经对购买历史数据进行了RFM分析.enter image description here

基于RFM类我已经对客户进行了细分,因为我的一些客户属于多个类别,我必须将客户细分为他们的所属类并添加到我的rfm表(添加单独的列'segment')

CardNo新近度频率货币R_Quartile F_Quartile M_Quartile RFMClass Segment

我的rfmtable列看起来像上面不包括列'segment' - 我想添加到rfm表

到目前为止我所做的是

for card_no in rfmSegmentation['CardNo']:
>     for num in rfmSegmentation['RFMClass']:
>         num = str(num)
>         if int(num[0])==1 and int(num[1])==1 and int(num[2])==1:
>             print("RFM_class",num,"cardno",card_no,"Best Customer")
>         elif int(num[1])==1:
>             print("RFM_class",num,"cardno",card_no,"Loyal Customer")
>         elif int(num[2])==1:
>             print("RFM_class",num,"cardno",card_no,"Big Spender")
>         elif int(num[0])==3 and int(num[1])==1 and int(num[2])==1:  
>             print("RFM_class",num,"cardno",card_no,"Almost Lost")        
>         elif int(num[0])==4 and int(num[1])==1 and int(num[2])==1:   
>             print("RFM_class",num,"cardno",card_no,"Lost Customer")
>         elif int(num[0])==4 and int(num[1])==4 and int(num[2])==4:  
>             print("RFM_class",num,"cardno",card_no,"Lost Cheap Customer")

它打印每个客户的段,但我想将其添加为rfmtable中的单独列并将其转换为csv文件

任何帮助,将不胜感激

python python-3.x pandas list dataframe
1个回答
0
投票
 > I think I found a solution
    > 
    > segment = []
    > 
    >  for row in rfmSegmentation['RFMClass']:
    >     row = str(row)
    >     if int(row[0])==1 and int(row[1])==1 and int(row[2])==1:
    >         segment.append('Best Customer')
    >     elif int(row[1])==1:
    >         segment.append('Loyal Customer')
    >     elif int(row[2])==1:
    >         segment.append('Big Spender')
    >     elif int(row[0])==3 and int(row[1])==1 and int(row[2])==1:
    >         segment.append('Almost Lost')
    >     elif int(row[0])==4 and int(row[1])==1 and int(row[2])==1: 
    >         segment.append('Lost Customer')
    >     elif int(row[0])==4 and int(row[1])==4 and int(row[2])==4:
    >         segment.append('Lost Cheap Customer')
    >     else:
    >         segment.append('Failed')
    >         
    >  rfmSegmentation['segment'] = segment 
© www.soinside.com 2019 - 2024. All rights reserved.