How To Make Day Of The Week Flags From Datetime Index In Pandas
I have a dataframe df whose index is in datetime format. I would like to make 7 new binary fields/columns indicating if the date is Monday, Tuesday, Wednesday, Thursday, Friday, S
Solution 1:
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.
We can then use pd.get_dummies(…)
[pandas-doc] to generate a "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.
For day names, you can use .day_name()
instead:
>>> 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
Post a Comment for "How To Make Day Of The Week Flags From Datetime Index In Pandas"