pandas 分组排名日期时间 [英] Pandas groupby rank date time

查看:97
本文介绍了 pandas 分组排名日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在日期时间排名方面遇到了问题。可以说我有下表。

  ID TIME 
01 2018-07-11 11:12:20
01 2018-07-12 12:00:23
01 2018-07-13 12:00:00
02 2019-09-11 11:00:00
02 2019- 09-12 12:00:00

,我想添加另一列以按时间对表进行排名对于每个ID和组。我用

  df ['RANK'] = data.groupby('ID')['TIME']。rank(升序= True)

但出现错误:

 'NoneType'对象不可调用

如果我替换日期时间转换为数字,是否有效......任何解决方案?

  data ['TIME'] = pd.to_datetime(data ['TIME'])
data ['RANK '] = data.groupby('ID')['TIME']。rank(升序为True)
打印(数据)
ID时间范围
0 1 2018-07-11 11 :12:20 1.0
1 1 2018-07-12 12:00:23 2.0
2 1 2018-07-13 12:00:00 3.0
3 2 2019-09-11 11:00:00 1.0
4 2 2019-09-12 12:00:00 2.0


i got an issue over ranking of date times. Lets say i have following table.

ID    TIME
01    2018-07-11 11:12:20
01    2018-07-12 12:00:23
01    2018-07-13 12:00:00
02    2019-09-11 11:00:00
02    2019-09-12 12:00:00

and i want to add another column to rank the table by time for each id and group. I used

df['RANK'] = data.groupby('ID')['TIME'].rank(ascending=True)

but get an error:

'NoneType' object is not callable

If i replace datetime to numbers, it works.... any solutions?

解决方案

For me working convert column to datetimes for avoid your error.

data['TIME'] = pd.to_datetime(data['TIME'])
data['RANK'] = data.groupby('ID')['TIME'].rank(ascending=True)
print (data)
   ID                TIME  RANK
0   1 2018-07-11 11:12:20   1.0
1   1 2018-07-12 12:00:23   2.0
2   1 2018-07-13 12:00:00   3.0
3   2 2019-09-11 11:00:00   1.0
4   2 2019-09-12 12:00:00   2.0

这篇关于 pandas 分组排名日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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