2

I want to transform my DataFrame with dates to the specific weekday.

df=
    Date
0   2019-09-05
1   2018-09-07
2   2017-09-02

Another column now should say e.g. Monday, Tuesday etc. for each day. How can I run this method in a Dataframe?

num3ri
  • 822
  • 16
  • 20
Minfetli
  • 303
  • 3
  • 12
  • 3
    Does this answer your question? [How do I get the day of week given a date?](https://stackoverflow.com/questions/9847213/how-do-i-get-the-day-of-week-given-a-date) – Pasindu Gamarachchi Dec 04 '19 at 10:00
  • @minfetli, This question may already have an answer here ! https://stackoverflow.com/questions/28009370/get-weekday-day-of-week-for-datetime-column-of-dataframe – Rahul Kr Daman Dec 04 '19 at 10:10

2 Answers2

0

You can do the following using pandas.Series.dt.day_name

df['Day'] = df['Date'].dt.day_name()

Giving output:

>>> df
        Date       Day
0 2019-09-05  Thursday
1 2018-09-07    Friday
2 2017-09-02  Saturday
CDJB
  • 14,043
  • 5
  • 29
  • 55
0

As another option you can convert dates to day name using .strftime

df['Day'] = df['Date'].strftime('%A') 

Please, note that dates must be in datetime format to perform this.

You can also find Python's strftime directives here

Porada Kev
  • 503
  • 11
  • 24