题解 | #牛客网用户练习的平均次日留存率#
牛客网用户练习的平均次日留存率
https://www.nowcoder.com/practice/1f9e2b9be96b4b27a2476f8f12c5d29f
import pandas as pd df = pd.read_csv('nowcoder.csv',sep=',',parse_dates=['date']) df['date'] = df.date.dt.date.astype('datetime64[D]') df = df.drop_duplicates(['user_id','date']).sort_values(['user_id','date']).reset_index(drop=True) total = df['user_id'].count() df.set_index('date',inplace=True) res = df.groupby('user_id')['question_id'].rolling('2D').count() num_act = res[res>=2].count() print(f'{(num_act/total):.2f}')