Num day to Name day with Pandas

后端 未结 4 2012
忘了有多久
忘了有多久 2021-02-13 21:47

If I use this funtion pd.DatetimeIndex(dfTrain[\'datetime\']).weekday I get number of the day, but I don\'t find any function which give the name of de day... So I

4条回答
  •  生来不讨喜
    2021-02-13 21:59

    Adding to the previous correct answer from @jezrael, you can use this:

    import calendar
    df['weekday'] = pd.Series(pd.Categorical(df['datetime'].dt.weekday_name, categories=list(calendar.day_name)))
    

    which also provides your new categorical variable with order (in this example: 'Monday', ..., 'Sunday') according to this. This will possibly be helpful on next steps of your analysis.

提交回复
热议问题