题解 | #牛客网用户练习的平均次日留存率#
牛客网用户练习的平均次日留存率
https://www.nowcoder.com/practice/1f9e2b9be96b4b27a2476f8f12c5d29f
import pandas as pd from datetime import timedelta nowcoder=pd.read_csv('nowcoder.csv',sep=',') #日期转换 nowcoder['date']=pd.to_datetime(nowcoder.date).dt.date #去重 nowcoder=nowcoder.drop_duplicates(subset=['user_id','date']) #复制,日期加一 nowcoder2=nowcoder.copy() nowcoder2['date']=nowcoder2['date']+timedelta(1) #留存数 num_re=pd.merge(nowcoder,nowcoder2,on=['user_id','date']).shape[0] #总计数 num_total=nowcoder.shape[0] #留存率 print(round(num_re/num_total,2))