jeudi 22 avril 2021

how to find total play time of each week for the given date in python?

I have a data frame that looks like the one below

k={'user_id':[1,1,1,1,1,2,2,2,3,3,3,3,3,4,4,4,5,5],
   'created':[ '2/09/2021','2/10/2021','2/16/2021','2/17/2021','3/09/2021','3/10/2021','3/18/2021','3/19/2021',
              '2/19/2021','2/20/2021','2/26/2021','2/27/2021','3/09/2021','2/10/2021','2/18/2021','3/19/2021',
             '3/24/2021','3/30/2021',],
   'stop_time':[11,12,13,14,15,25,26,27,6,7,8,9,10,11,12,13,25,26],
  'play_time':[10,11,12,13,14,24,25,26,5,6,7,8,9,10,11,13,24,25]}

df=pd.DataFrame(data=k)

df['created']=pd.to_datetime(df['created'], format='%m/%d/%Y')
df['total_play_time'] = df['stop_time'] - df['play_time']

enter image description here

Now we need to use the first date of each user_id as the first-week start date for example we need to select the '2/9/2021' is the first-week start date of user_id 1 and '3/09/2021' as the first-week start date of user_id 2.

we need sum the total playtime each week of user_id its continue to give a sum of each until the current date(for example if run report to today its has to give sum of each week until today) and give the result as below

ID  week1   week2     week3  week4  week5  week6 week7  week8     week9  week10  week11  week12
1   3        2        0      0      0      0     0      0         0       0       0      0
2   1        2        0      0      0      0     0

Aucun commentaire:

Enregistrer un commentaire