熊猫数据框分组日期时间月份

atomh33ls:

考虑一个csv文件:

string,date,number
a string,2/5/11 9:16am,1.0
a string,3/5/11 10:44pm,2.0
a string,4/22/11 12:07pm,3.0
a string,4/22/11 12:10pm,4.0
a string,4/29/11 11:59am,1.0
a string,5/2/11 1:41pm,2.0
a string,5/2/11 2:02pm,3.0
a string,5/2/11 2:56pm,4.0
a string,5/2/11 3:00pm,5.0
a string,5/2/14 3:02pm,6.0
a string,5/2/14 3:18pm,7.0

我可以读懂它,并将date列重新格式化为datetime格式:

b=pd.read_csv('b.dat')
b['date']=pd.to_datetime(b['date'],format='%m/%d/%y %I:%M%p')

我一直试图按月对数据进行分组。似乎应该有一种明显的方式来访问月份并以此进行分组。但是我似乎做不到。有人知道吗?

我目前正在尝试按日期重新建立索引:

b.index=b['date']

我可以这样访问月份:

b.index.month

但是我似乎找不到一个可以按月汇总的函数。

atomh33ls:

设法做到这一点:

b = pd.read_csv('b.dat')
b.index = pd.to_datetime(b['date'],format='%m/%d/%y %I:%M%p')
b.groupby(by=[b.index.month, b.index.year])

要么

b.groupby(pd.Grouper(freq='M'))  # update for v0.21+

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章