溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

python怎么計算回購率

發(fā)布時間:2022-04-25 10:55:54 來源:億速云 閱讀:206 作者:iii 欄目:大數(shù)據(jù)

這篇文章主要介紹“python怎么計算回購率”的相關(guān)知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“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')            # 計數(shù)    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

這個函數(shù)可以用來某月新客在次月的回購率, 如果要計算多個月的回購率,則需要循環(huán)。

首先生成  月份列表 ,月份間隔 為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'))

下面開始循環(huán)

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)

python怎么計算回購率

如果要計算本月新客在第二個月的回購率,則n_space=2

關(guān)于“python怎么計算回購率”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識,可以關(guān)注億速云行業(yè)資訊頻道,小編每天都會為大家更新不同的知識點。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI