How can I convert a Date Column in Python to a Day of the Week and a number?

52 Views Asked by At

I have two similar date columns named EventDate and Type of Day in the following format: 1/27/2023.

How do I change the second column into a day of the week. (E.g. Monday, Tuesday)

Thanks in advance.

I have tried several routes without avail. Like:

import datetime
datetime.datetime.strptime('Type of Day','%M/%d/%Y').strftime('%A'))
1

There are 1 best solutions below

0
Corralien On

I suppose you have a dataframe:

df['Type of Day'] = pd.to_datetime(df['EventDate']).dt.strftime('%A')
print(df)

# Output
   EventDate Type of Day
0  1/27/2023      Friday

For a single date:

import datetime

event_date = '1/27/2023'
type_of_day = datetime.datetime.strptime(event_date, '%m/%d/%Y').strftime('%A')
print(type_of_day)

# Output
Friday