I have a pandas dataframe:
How can I convert them as the "day of the week" and "day of the year" as new columns in the end?
I have a pandas dataframe:
How can I convert them as the "day of the week" and "day of the year" as new columns in the end?
Hope this help you.
import pandas as pd
data= {'reservation':
['2015-12-31',
'2015-12-29',
'2015-02-20',
'2015-10-28',
'2015-12-15',
'2015-09-17'
]}
df=pd.DataFrame(data)
df['day_of_week'] = pd.to_datetime(df['reservation']).dt.dayofweek
df['day_name'] = pd.to_datetime(df['reservation']).dt.day_name()
df['day_of_year'] = pd.to_datetime(df['reservation']).dt.dayofyear
print(df)
[Result]:
reservation day_of_week day_name day_of_year 0 2015-12-31 3 Thursday 365 1 2015-12-29 1 Tuesday 363 2 2015-02-20 4 Friday 51 3 2015-10-28 2 Wednesday 301 4 2015-12-15 1 Tuesday 349 5 2015-09-17 3 Thursday 260