Advertisement
Korotkodul

S_Прибыль

Oct 2nd, 2021 (edited)
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.58 KB | None | 0 0
  1. #В файле visits.csv зафиксированы даты посещения клиента.
  2. #Поля:
  3. #id - уникальный идентификатор клиента
  4. #start_dt - дата визита (первый визит совпадает с датой регистрации клиента)
  5.  
  6. #В файле purchases.csv находится информация о совершенных покупках клиентом.
  7. #Поля:
  8. #buy_ts — дата и время покупки
  9. #id - уникальный идентификатор клиента
  10. #revenue — доход в y.e.
  11.  
  12. #Посчитайте LTV на 30 день с момента прихода клиента. Постройте график динамики LTV по дням.
  13. #Для начала возьмём небольшую когорту -- 50 пользователей -- затем увеличим количество
  14.  
  15. #(1)Берем когорту пользователей и для каждого пользователя считаем прибыль в динамике по дням с момента регистрации.
  16.  
  17. import csv
  18. calendar = {
  19. 2016: {1: 31, 2: 29, 3: 31, 4: 30, 5: 31, 6: 30, 7: 31, 8: 31, 9: 30, 10: 31, 11: 30, 12: 31},
  20. 2017: {1: 31, 2: 28, 3: 31, 4: 30, 5: 31, 6: 30, 7: 31, 8: 31, 9: 30, 10: 31, 11: 30, 12: 31}
  21. }
  22.  
  23. def count_end_date(start_date, calendar_):
  24. year = start_date[0]
  25. month = start_date[1]
  26. day = start_date[2]
  27. end_year = year
  28. end_month = month
  29. end_day = day
  30. for i in range(30):
  31. end_day += 1
  32. if end_year not in calendar_.keys():
  33. continue
  34. if end_day > calendar_[end_year][end_month]:
  35. end_day = 1
  36. end_month += 1
  37. if end_month == 13:
  38. end_month = 1
  39. if end_month == 1:
  40. end_year += 1
  41. end_date = [end_year, end_month, end_day]
  42. return end_date
  43.  
  44. def check_period( date, start_date, end_date, calendar_):
  45. start_year = start_date[0]
  46. start_month = start_date[1]
  47. start_day = start_date[2]
  48. end_year = end_date[0]
  49. end_month = end_date[1]
  50. end_day = end_date[2]
  51. this_year = date[0]
  52. this_month = date[1]
  53. this_day = date[2]
  54. ans = False
  55. if this_month == start_month:
  56. if this_day <= calendar_[start_year][start_month]:
  57. ans = True
  58. elif this_month == end_month:
  59. if this_day <= end_day:
  60. ans = True
  61. return ans
  62.  
  63. from_date = {}
  64. to_date = {}
  65.  
  66.  
  67. def recycle(line): #get [id, date, income] from a row of csv
  68. if len(line) == 1 and type(line[0]) == str and '"' in line[0]:
  69. line = line[0]
  70. sz = len(line)
  71. num_quotes = 0
  72. income = ""
  73. for i in reversed(range(sz)):
  74. if line[i] == '"':
  75. num_quotes += 1
  76. elif num_quotes == 1:
  77. income += line[i]
  78. elif num_quotes == 2:
  79. break
  80. if income == '':
  81. return
  82. income = income[::-1]
  83. income = income.replace(',', '.')
  84. income = float(income)
  85.  
  86. date = line.split()[0]
  87. date = date.split('.')
  88. day = int(date[0])
  89. month = int(date[1])
  90. year = int(date[2])
  91. date = [year, month, day]
  92. id = line.split(',')
  93. id = id[1]
  94. elif len(line) == 1 and type(line[0]) == str and '"' not in line[0]:
  95. #['11.01.2017 10:52:11,2636761148,11']
  96. date, id, income = line[0].split(',')
  97. income = float(income)
  98. date = list(int(i) for i in date.split()[0].split('.')[::-1])
  99. else:
  100. income = float(line[-1])
  101. id = line[-2]
  102. date = line[0].split()[0].split('.')
  103. day = int(date[0])
  104. month = int(date[1])
  105. year = int(date[2])
  106. date = [year, month, day]
  107. return [id, date, income]
  108.  
  109. dynamic_income_per_user = {}
  110. with open("visits.csv", 'r', encoding = 'utf-8') as file:
  111. reader = csv.reader(file, delimiter = ',', lineterminator = '\r')
  112. u = 0
  113. for line in reader:
  114. u+=1
  115. id = line[0]
  116. date = line[1]
  117. date = list(int(i) for i in date.split('-')) #[year, month, day]
  118. if id not in from_date.keys():
  119. from_date[id] = date
  120. to_date[id] = count_end_date(date, calendar)
  121. dynamic_income_per_user[id] = [0] * 30
  122.  
  123.  
  124. def num_of_day_from_start(start_date, date, calendar_):
  125. start_year = start_date[0]
  126. start_month = start_date[1]
  127. start_day = start_date[2]
  128. this_year = date[0]
  129. this_month = date[1]
  130. this_day = date[2]
  131. days_till_start = 0
  132. days_till_this = 0
  133. #how many days till start
  134. for year in calendar_:
  135. for month in calendar_[year]:
  136. if month == start_month and year == start_year:
  137. days_till_start += start_day
  138. break
  139. else:
  140. days_till_start += calendar_[year][month]
  141. #how many days till this date
  142. for year in calendar_:
  143. for month in calendar_[year]:
  144. if month == this_month and year == this_year:
  145. days_till_this += this_day
  146. break
  147. else:
  148. days_till_this += calendar_[year][month]
  149. days_from_start_till_this_date = days_till_this - days_till_start
  150. return days_from_start_till_this_date
  151.  
  152.  
  153. with open("purchases.csv", 'r', encoding = 'utf-8') as file:
  154. reader = csv.reader(file, delimiter = ';', lineterminator = '\r')
  155. for line in reader:
  156. purchase = recycle(line) #[id, date, income]
  157. id = purchase[0]
  158. date = purchase[1]
  159. income = purchase[2]
  160. start_date = from_date[id]
  161. end_date = to_date[id]
  162. in_30_days_from_start = check_period(date, start_date, end_date, calendar)
  163. if in_30_days_from_start == True:
  164. day_from_start = num_of_day_from_start(start_date, date, calendar)
  165. if day_from_start not in range(0,30):
  166. continue
  167. print(day_from_start)
  168. if id in dynamic_income_per_user:
  169. dynamic_income_per_user[id][day_from_start] += income
  170.  
  171. #теперь у нас есть данные по "динамической" прибыли о каждого покупателя
  172. print(dynamic_income_per_user)
  173. #Идея на завтра: записать DictWriter --> дни от 1 до 30 с регистрации -->DictReader -->считать сумму и т.д .
  174. #ИЛИ можно обойтись без этого???
  175. #Кстати, почему код don't run but debug???
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement