def multi(uid):
tmp = log[log.user_id==uid]
ct = pd.crosstab(tmp.order_number, tmp.product_id).reset_index().set_index('order_number')
li = []
for pid in ct.columns:
streak = 0
sw_odr = False
for onb,odr in enumerate(ct[pid].values):
onb+=1
if sw_odr == False and odr == 1:
sw_odr = True
streak = 1
li.append([uid, pid, onb, streak])
continue
if sw_odr == True:
if odr == 1 and streak>0:
streak += 1
li.append([uid, pid, onb, streak])
elif odr == 1 and streak<=0:
streak = 1
li.append([uid, pid, onb, streak])
elif odr == 0 and streak>0:
streak = 0
li.append([uid, pid, onb, streak])
elif odr == 0 and streak<=0:
streak -= 1
li.append([uid, pid, onb, streak])
return pd.DataFrame(li, columns=['user_id', 'product_id', 'order_number', 'streak'])
评论列表
文章目录