Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does python calculate the buyback rate

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of "how python calculates the repurchase rate". The editor shows you the operation process through the actual case. The operation method is simple, fast and practical. I hope this article "how to calculate the repurchase rate of python" can help you solve the problem.

Calculate the repurchase rate of new customers in period An in period B.

Def huigou (saleflow,former_quarter_start_end= ['2019-10-01 00 groupby_list_vip groupby_list_vip = [' shopid_cardid','shopid_branch',' Rank'], groupby_list_branch= ['2019-01-01 00 def huigou], groupby_list_vip = [' shopid_cardid','shopid_branch',' Rank'], groupby_list_branch= [ 'Rank']): # each first purchase That is, the new customer's pipeline saleflow_first = saleflow.groupby (groupby_list_vip). Oper_date.min (). Reset_index (). Rename (columns= {'oper_date':'date_1st'}) # pick out the new customers in the previous quarter former_new = saleflow_first [(saleflow_first [' date_1st'] pd.to_datetime (former_quarter_start_ end [0])] # after picking out After_flow = saleflow in the first quarter [(saleflow ['oper_date'] pd.to_datetime (after_quarter_start_ end [0])] # # join shows the pipelining fugou_flow = former_new.merge (after_flow) of members in the previous quarter How='right') # count 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

This function can be used to calculate the repurchase rate of new customers in the following month. If you want to calculate the repurchase rate for multiple months, you need to cycle.

First generate a list of months with a month interval of 1, and the repurchase rate column name is' next_'+ str (n_space) +'_ month_rebuy_rate'

Create a new empty dataframe

# calculate the monthly return rate of each month, that is, the January return rate import arrowmonth_list = [each.format ('YYYY-MM-DD HH:mm:SS') for each in arrow.Arrow.range (' month', arrow.get ('2019-07-01 00purl 00lv 00lv 00lb HH:mm:SS') YYYYYMMMI DD HH:mm:SS') Arrow.get ('2020-05-01 00 Velcro 00D HH:mm:SS')] nasty spaceways 1huigoufang name =' next_'+ str (n_space) +'_ month_rebuy_rate'df_empty = pd.DataFrame (columns= ('pinpai',' shopid_cardid_new_count')) 'shopid_cardid_back_count',huigou_name,' current month', 'shopId'))

Let's start the loop

For i in range (len (month_list)-n_space-1):

Print (print 'current month:', 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 ['current month'] = month_ list [I] [0:10] result ['shopId'] = shopId df_empty=df_empty.append (result)

If you want to calculate the repurchase rate of this month's new customers in the second month, then n_space=2

This is the end of the content about "how python calculates the buyback rate". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report