这篇文章主要介绍“python怎么计算回购率”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“python怎么计算回购率”文章能帮助大家解决问题。
计算 A时间段的新客 在B时间段的回购率
def huigou(saleflow,former_quarter_start_end=['2019-10-01 00:00:00','2020-01-01 00:00:00'],
after_quarter_start_end=['2020-01-01 00:00:00','2020-04-01 00:00:00'] ,
groupby_list_vip =['shopid_cardid','shopid_branch','段位'],
groupby_list_branch=['shopid_branch','段位']):
#每个第一次购买 , 也就是 新客的流水
saleflow_first = saleflow.groupby(groupby_list_vip).oper_date.min().reset_index().rename(columns={'oper_date':'date_1st'})
# 挑选出 前面季度的新客
former_new =saleflow_first[(saleflow_first['date_1st']<pd.to_datetime(former_quarter_start_end[1]))&(saleflow_first['date_1st']>pd.to_datetime(former_quarter_start_end[0]))]
#挑选出 后一季度的流水
after_flow = saleflow[(saleflow['oper_date']<pd.to_datetime(after_quarter_start_end[1]))&(saleflow['oper_date']>pd.to_datetime(after_quarter_start_end[0]))]
## join 出 前面季度会员在后一季度的流水
fugou_flow = former_new.merge(after_flow,how='right')
# 计数
former_NEW_count=saleflow_first.groupby(groupby_list_branch).shopid_cardid.nunique().reset_index()\
.rename(columns={'shopid_cardid':'shopid_cardid_new_count'})
after_BACK_count= fugou_flow.groupby(groupby_list_branch).shopid_cardid.nunique().reset_index().\
rename(columns={'shopid_cardid':'shopid_cardid_back_count'})
df = former_NEW_count.merge(after_BACK_count,how='left')
return df
这个函数可以用来某月新客在次月的回购率, 如果要计算多个月的回购率,则需要循环。
首先生成 月份列表 ,月份间隔 为1 , 回购率列名 叫做 'next_'+ str(n_space)+'_month_rebuy_rate'
新建空的dataframe
# 计算每个月的 次月回头率 即1月回头率import arrowmonth_list =[each.format('YYYY-MM-DD HH:mm:SS') for each in arrow.Arrow.range('month', arrow.get('2019-07-01 00:00:00','YYYY-MM-DD HH:mm:SS'), arrow.get('2020-05-01 00:00:00','YYYY-MM-DD HH:mm:SS'))]n_space=1huigou_name= 'next_'+ str(n_space)+'_month_rebuy_rate'df_empty =pd.DataFrame(columns=('pinpai', 'shopid_cardid_new_count', 'shopid_cardid_back_count',huigou_name, '当前月', 'shopId'))
下面开始循环
for i in range(len(month_list)-n_space-1):
print(i,'当前月:',month_list[i])
ss = huigou(yyp_vipflow ,former_quarter_start_end=[month_list[i],month_list[i+n_space]],
after_quarter_start_end= [ month_list[i+n_space] , month_list[i+n_space+1] ] ,
groupby_list_vip =['shopid_cardid','shopid_branch','pinpai'],
groupby_list_branch=['shopid_branch','pinpai'])
new_sum = ss.groupby(['pinpai']).shopid_cardid_new_count.sum().reset_index()
back_sum =ss.groupby(['pinpai']).shopid_cardid_back_count.sum().reset_index()
result = new_sum.merge(back_sum)
result[huigou_name] = result['shopid_cardid_back_count'] / result['shopid_cardid_new_count']
result['当前月'] =month_list[i][0:10]
result['shopId'] =shopId
df_empty=df_empty.append(result)
如果要计算本月新客在第二个月的回购率,则n_space=2
关于“python怎么计算回购率”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注亿速云行业资讯频道,小编每天都会为大家更新不同的知识点。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。