0

I have a pandas dataframe:

enter image description here

How can I convert them as the "day of the week" and "day of the year" as new columns in the end?

outsider
  • 135
  • 5
  • 1
    https://stackoverflow.com/questions/25146121/extracting-just-month-and-year-separately-from-pandas-datetime-column check this kind of question you'll find your answer – ombk Nov 27 '20 at 19:41
  • 1
    `pd.to_datetime(df.reservation...)` – ombk Nov 27 '20 at 19:41

1 Answers1

1

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
AziMez
  • 2,014
  • 1
  • 6
  • 16