我如何从熊猫的时间戳中提取星期几

时间:2018-07-30 02:00:31

标签: python-3.x pandas

我在数据帧中有以下时间戳列,我想从中创建另一个名为day of week的列。怎么做

Input

 Pickup date/time    
07/05/2018 09:28:00                     
14/05/2018 17:00:00                      
15/05/2018 17:00:00                      
15/05/2018 17:00:00                     
23/06/2018 17:00:00                     
29/06/2018 17:00:00  

预期产量

Pickup date/time      Day of Week
07/05/2018 09:28:00     Monday                
14/05/2018 17:00:00     Monday                  
15/05/2018 17:00:00     Tuesday                 
15/05/2018 17:00:00     Tuesday               
23/06/2018 17:00:00     Saturday              
29/06/2018 17:00:00     Friday

1 个答案:

答案 0 :(得分:2)

您可以使用weekday_name

df['date/time'] = pd.to_datetime(df['date/time'], format = '%d/%m/%Y %H:%M:%S')

df['Day of Week'] = df['date/time'].dt.weekday_name

你得到

    date/time   Day of Week
0   2018-05-07 09:28:00 Monday
1   2018-05-14 17:00:00 Monday
2   2018-05-15 17:00:00 Tuesday
3   2018-05-15 17:00:00 Tuesday
4   2018-06-23 17:00:00 Saturday
5   2018-06-29 17:00:00 Friday