How do I get the day of week given a date?

前端 未结 26 1472
迷失自我
迷失自我 2020-11-22 04:40

I want to find out the following: given a date (datetime object), what is the corresponding day of the week?

For instance, Sunday is the first day, Mond

26条回答
  •  臣服心动
    2020-11-22 05:25

    If you want to generate a column with a range of dates (Date) and generate a column that goes to the first one and assigns the Week Day (Week Day), do the following (I will used the dates ranging from 2008-01-01 to 2020-02-01):

    import pandas as pd
    dr = pd.date_range(start='2008-01-01', end='2020-02-1')
    df = pd.DataFrame()
    df['Date'] = dr
    df['Week Day'] = pd.to_datetime(dr).weekday
    

    The output is the following:

    The Week Day varies from 0 to 6, where 0 corresponds to Monday and 6 to Sunday.

提交回复
热议问题