Groupby first two earliest dates, then average time between first two dates - pandas

后端 未结 3 735
闹比i
闹比i 2021-01-27 17:43

I\'m hoping to groupby users and find the first two uploads. I\'ve figured out how to get the first date via minimum, but I\'m having trouble getting that second upload date. Th

3条回答
  •  生来不讨喜
    2021-01-27 18:25

    Since the other answers explain pretty well how to achieve this, I'll give you a one-liner for a change

     In [1]: df.groupby('User_ID').apply(lambda g: g.sort_values('Date_Uploaded')['Date_Uploaded'][:2].diff()).mean()
     Out[1]: Timedelta('21 days 12:00:00')
    

提交回复
热议问题