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

前端 未结 26 1428
迷失自我
迷失自我 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:14

    Say you have timeStamp: String variable, YYYY-MM-DD HH:MM:SS

    step 1: convert it to dateTime function with blow code...

    df['timeStamp'] = pd.to_datetime(df['timeStamp'])
    

    Step 2 : Now you can extract all the required feature as below which will create new Column for each of the fild- hour,month,day of week,year, date

    df['Hour'] = df['timeStamp'].apply(lambda time: time.hour)
    df['Month'] = df['timeStamp'].apply(lambda time: time.month)
    df['Day of Week'] = df['timeStamp'].apply(lambda time: time.dayofweek)
    df['Year'] = df['timeStamp'].apply(lambda t: t.year)
    df['Date'] = df['timeStamp'].apply(lambda t: t.day)
    

提交回复
热议问题