[英]How to make day of the week flags from datetime index in pandas
I have a dataframe df
whose index is in datetime format.我有一个 dataframe
df
,其索引为日期时间格式。 I would like to make 7 new binary fields/columns indicating if the date is Monday, Tuesday, Wednesday, Thursday, Friday, Saturday or Sunday.我想创建 7 个新的二进制字段/列,指示日期是周一、周二、周三、周四、周五、周六还是周日。 So column Monday would have a 1 in it if it's a Monday and a 0 otherwise, for example.
因此,例如,如果星期一是星期一,列星期一将有一个 1,否则为 0。
Is there a neat way of doing this?有没有一种巧妙的方法来做到这一点? I have been trying but my solutions are very ugly.
我一直在尝试,但我的解决方案非常难看。
If the data frame is for example:如果数据框是例如:
>>> df
date
0 2018-01-01
1 2018-01-02
2 2018-01-03
3 2018-01-04
4 2018-01-05
5 2018-01-06
6 2018-01-07
7 2018-01-08
8 2018-01-09
9 2018-01-10
10 2018-01-11
11 2018-01-12
12 2018-01-13
13 2018-01-14
14 2018-01-15
15 2018-01-16
16 2018-01-17
17 2018-01-18
18 2018-01-19
19 2018-01-20
We can use df['date'].dt.weekday
to obtain a number between 0
and 6
that specifies the day of the week.我们可以使用
df['date'].dt.weekday
来获取一个介于0
和6
之间的数字,它指定星期几。
We can then use pd.get_dummies(…)
[pandas-doc] to generate a "one-hot encoding":然后我们可以使用
pd.get_dummies(…)
[pandas-doc]来生成“one-hot encoding”:
>>> pd.concat((df, pd.get_dummies(df['date'].dt.weekday)), axis=1)
date 0 1 2 3 4 5 6
0 2018-01-01 1 0 0 0 0 0 0
1 2018-01-02 0 1 0 0 0 0 0
2 2018-01-03 0 0 1 0 0 0 0
3 2018-01-04 0 0 0 1 0 0 0
4 2018-01-05 0 0 0 0 1 0 0
5 2018-01-06 0 0 0 0 0 1 0
6 2018-01-07 0 0 0 0 0 0 1
7 2018-01-08 1 0 0 0 0 0 0
8 2018-01-09 0 1 0 0 0 0 0
9 2018-01-10 0 0 1 0 0 0 0
10 2018-01-11 0 0 0 1 0 0 0
11 2018-01-12 0 0 0 0 1 0 0
12 2018-01-13 0 0 0 0 0 1 0
13 2018-01-14 0 0 0 0 0 0 1
14 2018-01-15 1 0 0 0 0 0 0
15 2018-01-16 0 1 0 0 0 0 0
16 2018-01-17 0 0 1 0 0 0 0
17 2018-01-18 0 0 0 1 0 0 0
18 2018-01-19 0 0 0 0 1 0 0
19 2018-01-20 0 0 0 0 0 1 0
Here 0
, 1
, 2
, etc. are the day-of-the-week numbers.这里
0
、 1
、 2
等是星期几。
For day names, you can use .day_name()
instead:对于日期名称,您可以使用
.day_name()
代替:
>>> pd.concat((df, pd.get_dummies(df['date'].dt.day_name())), axis=1)
date Friday Monday Saturday Sunday Thursday Tuesday Wednesday
0 2018-01-01 0 1 0 0 0 0 0
1 2018-01-02 0 0 0 0 0 1 0
2 2018-01-03 0 0 0 0 0 0 1
3 2018-01-04 0 0 0 0 1 0 0
4 2018-01-05 1 0 0 0 0 0 0
5 2018-01-06 0 0 1 0 0 0 0
6 2018-01-07 0 0 0 1 0 0 0
7 2018-01-08 0 1 0 0 0 0 0
8 2018-01-09 0 0 0 0 0 1 0
9 2018-01-10 0 0 0 0 0 0 1
10 2018-01-11 0 0 0 0 1 0 0
11 2018-01-12 1 0 0 0 0 0 0
12 2018-01-13 0 0 1 0 0 0 0
13 2018-01-14 0 0 0 1 0 0 0
14 2018-01-15 0 1 0 0 0 0 0
15 2018-01-16 0 0 0 0 0 1 0
16 2018-01-17 0 0 0 0 0 0 1
17 2018-01-18 0 0 0 0 1 0 0
18 2018-01-19 1 0 0 0 0 0 0
19 2018-01-20 0 0 1 0 0 0 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.