kxe*_*001 4 python date rank 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
Run Code Online (Sandbox Code Playgroud)
我想添加另一列以按时间为每个 ID 和组对表进行排名。我用了
df['RANK'] = data.groupby('ID')['TIME'].rank(ascending=True)
Run Code Online (Sandbox Code Playgroud)
但得到一个错误:
'NoneType' object is not callable
Run Code Online (Sandbox Code Playgroud)
如果我将日期时间替换为数字,它会起作用......任何解决方案?
对我来说,将列转换为日期时间以避免您的错误。
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5921 次 |
| 最近记录: |