在 pandas 中,如何按工作日()分组datetime列? [英] in pandas how can I groupby weekday() for a datetime column?

查看:67
本文介绍了在 pandas 中,如何按工作日()分组datetime列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想过滤掉周末数据,而只查看工作日的数据(mon(0)-fri(4)).我是熊猫新手,在熊猫中做到这一点的最佳方法是什么?

I'd like to filter out weekend data and only look at data for weekdays (mon(0)-fri(4)). I'm new to pandas, what's the best way to accomplish this in pandas?

import datetime
from pandas import *

data = read_csv("data.csv")
data.my_dt 

Out[52]:
0     2012-10-01 02:00:39
1     2012-10-01 02:00:38
2     2012-10-01 02:01:05
3     2012-10-01 02:01:07
4     2012-10-01 02:02:03
5     2012-10-01 02:02:09
6     2012-10-01 02:02:03
7     2012-10-01 02:02:35
8     2012-10-01 02:02:33
9     2012-10-01 02:03:01
10    2012-10-01 02:08:53
11    2012-10-01 02:09:04
12    2012-10-01 02:09:09
13    2012-10-01 02:10:20
14    2012-10-01 02:10:45
...

我想做类似的事情:

weekdays_only = data[data.my_dt.weekday() < 5]

AttributeError:'numpy.int64'对象没有属性'weekday'

AttributeError: 'numpy.int64' object has no attribute 'weekday'

但这是行不通的,我还不太了解如何访问列日期时间对象.

but this doesn't work, I haven't quite grasped how column datetime objects are accessed.

最终目标是按层次排列到工作日的小时范围,例如:

The eventual goal being to arrange hierarchically to weekday hour-range, something like:

monday, 0-6, 7-12, 13-18, 19-23
tuesday, 0-6, 7-12, 13-18, 19-23

推荐答案

您对函数工作日"的调用不起作用,因为它对data.my_dt的索引进行操作,该索引是一个int64数组(这是错误的地方消息来自)

your call to the function "weekday" does not work as it operates on the index of data.my_dt, which is an int64 array (this is where the error message comes from)

您可以使用以下方法在包含工作日的数据中创建一个新列:

you could create a new column in data containing the weekdays using something like:

data['weekday'] = data['my_dt'].apply(lambda x: x.weekday())

然后您可以使用以下方法过滤工作日:

then you can filter for weekdays with:

weekdays_only = data[data['weekday'] < 5 ]

我希望这对您有帮助

这篇关于在 pandas 中,如何按工作日()分组datetime列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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