我到处找了,但还没有找到一个好办法。我有一个熊猫数据框和我自己的短信数据。它有“utctime”、“sender”、“recipient”和“message”列。我想做的是将其按任意的分钟时间段(例如10或20分钟)进行分组,然后在多年内查看我在一天中每个时间段发送的消息量。你知道吗
数据的前几行看起来是这样的,这又延续了2年:
utctime sender recipient message
0 2016-11-25 18:00:25.877000+00:00 Person1 Me message1
1 2016-11-25 18:50:58.694000+00:00 Person2 Me message2
2 2016-11-25 18:54:41.871000+00:00 Me Person2 message3
3 2016-11-25 18:54:58.583000+00:00 Person2 Me message4
4 2016-11-25 19:01:19.694000+00:00 Me Person2 message5
什么是最好的方法,这样我就可以看到我自己的短信频率在20分钟的增量从00:00到23:40?你知道吗
到目前为止,我所做的是有另一个功能将时间压缩为一天:
def time_of_day(data, tz_info):
utctime = data.utctime.dt.tz_convert(tz_info)
data['timeofday'] = pd.to_datetime({'year': 2017,
'month': 1,
'day': 1,
'hour': utctime.dt.hour,
'minute': utctime.dt.minute,
'second': utctime.dt.second})
return data
然后我使用一个定制的石斑鱼对其进行分组:
sf = pytz.timezone('US/Pacific')
timedelta = '10min'
grouper = pd.Grouper(key='timeofday', freq=timedelta)
data = time_of_day(data, sf) # data is returned in time of day for tz
freq = data.groupby(grouper).count()
有没有更好的办法?你知道吗
IIUC,您需要从“utctime”中提取小时和分钟,将分钟分类到
bins
,并对count
消息的小时和分钟执行groupby
:相关问题 更多 >
编程相关推荐