大 pandas 数据帧groupby datetime月 [英] pandas dataframe groupby datetime month

查看:140
本文介绍了大 pandas 数据帧groupby datetime月的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑一个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

我可以阅读这个,并重新格式化日期列为日期时间格式:

I can read this in, and reformat the date column into datetime format:

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

我一直在尝试按月分组数据。似乎应该有一个明显的方式来访问这个月份,并将其分组。但我似乎不能这样做。有人知道吗?

I have been trying to group the data by month. It seems like there should be an obvious way of accessing the month and grouping by that. But I can't seem to do it. Does anyone know how?

我目前正在尝试的是按日期重新索引:

What I am currently trying is re-indexing by the date:

b.index=b['date']

我可以访问月份如下:

b.index.month

然而,我看起来似乎找不到一个月的功能。

However I can't seem to find a function to lump together by month.

推荐答案

管理这样做:

pd.groupby(b,by=[b.index.month,b.index.year])

df.groupby(pd.TimeGrouper(freq='M'))

这篇关于大 pandas 数据帧groupby datetime月的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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