Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
896 B
3
Indexable
Never
mobile_dataset['dt']=pd.to_datetime(mobile_dataset['event_time']).dt.date
result_data=mobile_dataset.merge(mobile_sources[['user_id','source']],on='user_id',how='left')
session_start = result_data.groupby(['user_id'])['dt'].min().reset_index()
session_start.rename(columns = {'dt':'session_start'}, inplace = True )
result_data=result_data.merge(session_start)
result_data['lifetime']=(result_data['dt']-result_data['session_start']).dt.days
result_grouped=result_data.pivot_table(index=['dt'],columns='lifetime',values='user_id',aggfunc='nunique')
cohort_sizes=(result_data.groupby('dt').agg({'user_id':'nunique'}).rename(columns={'user_id':'cohort_size'}))
cohort_sizes
result_grouped=cohort_sizes.merge(result_grouped,on='dt',how='left').fillna(0)
result_grouped
result_grouped=result_grouped.div(result_grouped['cohort_size'],axis=0).drop(columns=['cohort_size'])
result_grouped