in pandas how can I groupby weekday() for a datetime column?

前端 未结 2 1395
不知归路
不知归路 2021-02-01 22:37

I\'d like to filter out weekend data and only look at data for weekdays (mon(0)-fri(4)). I\'m new to pandas, what\'s the best way to accomplish this in pandas?

         


        
2条回答
  •  花落未央
    2021-02-01 22:52

    Faster way would be to use DatetimeIndex.weekday, like so:

    temp = pd.DatetimeIndex(data['my_dt'])
    data['weekday'] = temp.weekday
    

    Much much faster, especially for a large number of rows. For further info, check this answer.

提交回复
热议问题