您好,登錄后才能下訂單哦!
這篇文章主要介紹“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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。