-1

I have a pandas dataframe with one column called "date" with dates from 10/1/2018 to 5/1/2020 and one column called "sales" with total sales of the day. I set "date" as index and I want to have another column to show the day of the dates (e.g. Monday, Tuesday, Wednesday...). Is there any dataframe function to do that?

Thank you.

Emma Y
  • 1

3 Answers3

1

assuming your 'date' column is in proper datetime format (use pd.to_datetime() if not:

df['day_of_week'] = df['date'].dt.day_name()
yulGM
  • 894
  • 1
  • 5
  • 14
0

You can use calendar.weekday:

Returns the day of the week (0 is Monday) for year (1970–…), month (1–12), day (1–31).

Or datetime.weekday:

Return the day of the week as an integer, where Monday is 0 and Sunday is 6. The same as self.date().weekday(). See also isoweekday().

For dataframe, take a look at https://stackoverflow.com/a/55648922/1513933

Laurent LAPORTE
  • 21,958
  • 6
  • 58
  • 103
0

You can use dt.day_name as answered here.

Briefly, it works like....

df['day_of_the_week'] = df['date'].dt.day_name()
bdempe
  • 308
  • 2
  • 9