Advertisement
Vitaliy_Novichikhin

БП 2.4.1

Jul 17th, 2022
1,288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.93 KB | None | 0 0
  1. import pandas as pd
  2.  
  3. sessions = pd.read_csv('sessions.csv')
  4. sessions['session_start'] = pd.to_datetime(sessions['session_start'])
  5.  
  6.  
  7. def get_profiles(sessions):
  8.  
  9.     profiles = (
  10.         sessions.sort_values(by=['user_id', 'session_start'])
  11.         .groupby('user_id')
  12.         .agg(
  13.             {
  14.                 'session_start': 'first',
  15.                 'channel': 'first',
  16.                 'device': 'first',# допишите код
  17.                 'region': 'first'
  18.                 #'user_id': 'nunique'# допишите код
  19.             }
  20.         )
  21.         .rename(columns={'session_start': 'first_ts'})
  22.         .reset_index()
  23.     )
  24.  
  25.     profiles['dt'] = profiles['first_ts'].dt.date
  26.     profiles['month'] = profiles['first_ts'].astype('datetime64[M]')
  27.  
  28.     return profiles
  29.  
  30.  
  31. profiles = get_profiles(sessions)
  32.  
  33. print(
  34.     profiles.groupby('device'),
  35.     profiles.groupby('region') # допишите код
  36. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement