python - Pandas groupby rank date time
问题描述
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?
解决方案
对我来说,将列转换为日期时间以避免您的错误。
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
推荐阅读
- oop - 考虑到依赖注入,我应该在哪里构建嵌套逻辑层中的对象?
- python - 使用 BeautifulSoup 从 Web 上抓取 API 并部署在 heroku 上
- typescript - 打字稿:如何从可能是也可能不是数组元素的泛型中推断类型
- r - 如何有选择地从数据框中的元素中删除字符?
- volttron - 如何在 Volttron-GS 中设置 ILC 代理?
- file - Applescript 选择文件夹中的最后一个文件(按名称升序列出)
- c# - 如何在 confuserex 中使用密码混淆符号名称?
- powershell - 管道中的具体类型输出变量无效的 JObject
- css - 如何使每个徽标在视觉上都具有相同的大小?
- javascript - 模拟路由器模块笑话