【问题标题】:segment customers based on RFM score and create pandas DataFrame根据 RFM 分数细分客户并创建 pandas DataFrame
【发布时间】:2018-08-31 21:59:06
【问题描述】:

我已经对购买历史数据进行了 RFM 分析。

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

CardNo Recency Frequency Monetary R_Quartile F_Quartile M_Quartile RFMClass Segment

我的 rfmtable 列看起来像上面的列,不包括列“段”——我想添加到 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 文件

任何帮助将不胜感激

【问题讨论】:

  • 这实际上是一个pandas & dataframe 的问题,和machine-learning 无关(标签去掉);还可以考虑将标题更改为描述实际问题...
  • 完成............
  • 您能否发布一个具有所需输出的数据示例?请不要使用图片。
  • 你可以使用apply函数。

标签: python python-3.x pandas list dataframe


【解决方案1】:
 > 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 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-08
    • 2020-03-19
    • 1970-01-01
    • 2017-10-22
    • 2021-04-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多