-2

In my df I have a field called day for day and month such as, 2019/07/29 if I want to create another field in the df that states the actual day of week this is such as Sunday or Monday is that possible?

Thanks!

Chris90
  • 1,868
  • 5
  • 20
  • 42

1 Answers1

1

You need to use day_name.

Consider following example:

df = pd.DataFrame({
    'Date': ['2019/07/29','2019/01/20','2019/09/3']
})

Gives df:

    Date
0   2019/07/29
1   2019/01/20
2   2019/09/3

Add a column with weekday.

df['Weekday'] = pd.to_datetime(df['Date']).dt.day_name()

Output:

         Date   Weekday
0   2019/07/29  Monday
1   2019/01/20  Sunday
2   2019/09/3   Tuesday

ideally, you want to convert datetime series to a Datetime Object first and then do anything with it.

# df['Date'] = pd.to_datetime(df['Date'])
# df['Weekday'] = df['Date'].dt.day_name()
harvpan
  • 8,571
  • 2
  • 18
  • 36