RFM

RFM

1. 导入数据

import pandas as pd
import numpy as np
trad_flow = pd.read_csv('RFM_TRAD_FLOW.csv', encoding='gbk')
trad_flow.head()
transIDcumidtimeamounttype_labeltype
0233511000627SEP09:20:10:2258.0特价Special_offer
1233723003127SEP09:21:33:3569.0特价Special_offer
2234474010228SEP09:21:12:3469.0特价Special_offer
3234482017328SEP09:21:12:3469.0特价Special_offer
4234494001728SEP09:21:13:4869.0特价Special_offer

2.计算 RFM

M = pd.pivot_table(trad_flow, index='cumid', columns='type', values='amount', aggfunc='sum')
M.head()
typeNormalPresentedSpecial_offerreturned_goods
cumid
100013608.00.0420.0-694.0
100021894.00.0NaN-242.0
100033503.00.0156.0-224.0
100042979.00.0373.0-40.0
100052368.00.0NaN-249.0
F = pd.pivot_table(trad_flow, index='cumid', columns='type', values='transID', aggfunc='count')
F.head()
typeNormalPresentedSpecial_offerreturned_goods
cumid
1000115.08.02.02.0
1000212.05.0NaN1.0
1000315.08.01.01.0
1000415.012.02.01.0
100058.05.0NaN1.0
R = pd.pivot_table(trad_flow, index='cumid', columns='type', values='time', aggfunc='max')
R.head()
typeNormalPresentedSpecial_offerreturned_goods
cumid
1000121JUL09:09:31:2631MAR10:20:29:4812OCT09:10:59:1310JUL10:20:41:54
1000229JUL09:19:21:4131MAY09:14:19:33None04SEP09:14:51:57
1000331AUG09:16:11:0127JUN10:18:25:5502JAN10:20:19:3906JUN10:14:29:04
1000426NOV09:20:38:3229AUG10:12:44:5220AUG09:20:00:5203JUN10:21:16:01
1000529MAY10:15:49:2230JUN10:22:08:49None08APR10:19:53:48

3.衡量客户对打折商品的偏好

M['Special_offer'] = M['Special_offer'].fillna(0)
M['spe_ratio'] = M['Special_offer'] / (M['Special_offer'] + M['Normal'])
M_rank = M.sort_values('spe_ratio', ascending=False, na_position='last').head()
M_rank
typeNormalPresentedSpecial_offerreturned_goodsspe_ratio
cumid
10151765.00.0870.0NaN0.532110
400331206.00.0761.0-848.00.386884
402361155.00.0691.0-793.00.374323
302251475.00.0738.0-301.00.333484
200681631.00.0731.0-239.00.309483
M_rank['spe_ratio_group'] = pd.qcut(M_rank['spe_ratio'], 4) # 这里以age_oldest_tr字段等宽分为4段
M_rank.head()
typeNormalPresentedSpecial_offerreturned_goodsspe_ratiospe_ratio_group
cumid
10151765.00.0870.0NaN0.532110(0.387, 0.532]
400331206.00.0761.0-848.00.386884(0.374, 0.387]
402361155.00.0691.0-793.00.374323(0.333, 0.374]
302251475.00.0738.0-301.00.333484(0.308, 0.333]
200681631.00.0731.0-239.00.309483(0.308, 0.333]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值