Groupby 前两个最早日期,然后是前两个日期之间的平均时间 - pandas [英] Groupby first two earliest dates, then average time between first two dates - pandas

查看:48
本文介绍了Groupby 前两个最早日期,然后是前两个日期之间的平均时间 - pandas 的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望对用户进行分组并找到前两个上传.我已经想出了如何通过最小值获得第一个日期,但是我无法获得第二个上传日期.然后想获取所有用户的两个上传日期之间的平均时间.

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. Then would like to get the average time between the two upload dates for all users.

df:

Date_Uploaded  User_ID  Display_Status
2018-10-27     abc123   Cleared
2018-10-28     abc123   Cleared
2018-10-29     abc123   Pending
2018-09-21     abc123   Pending
2018-08-24     efg123   Pending
2018-08-01     efg123   Pending
2018-07-25     efg123   Pending

推荐答案

Using sort_values + head

df.sort_values('Date_Uploaded').groupby('User_ID').head(2)
Out[152]: 
  Date_Uploaded User_ID Display_Status
6    2018-07-25  efg123        Pending
5    2018-08-01  efg123        Pending
3    2018-09-21  abc123        Pending
0    2018-10-27  abc123        Cleared

这篇关于Groupby 前两个最早日期,然后是前两个日期之间的平均时间 - pandas 的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆