2017-10-12 46 views
1

密謀這裏是我的數據獲取週數是準備在熊貓

我是做sheet2['device_create_week'] = sheet2['device_create_at'].dt.week這裏的結果

 device_create_at  device_create_week 
136  2014-08-27 17:29:23  35 
245  2015-09-06 15:46:00  39 
257  2014-09-29 22:26:34  40 
258  2014-11-05 13:02:18  40 
480  2020-02-02 17:59:54  6 
481  2020-02-02 17:59:54  6 
482  2020-02-02 17:59:54  6 

但我想

 device_create_at  device_create_week 
136  2014-08-27 17:29:23  2014-35 
245  2015-09-06 15:46:00  2015-39 
257  2014-09-29 22:26:34  2014-40 
258  2014-11-05 13:02:18  2014-40 
480  2020-02-02 17:59:54  2020-6 
481  2020-02-02 17:59:54  2020-6 
482  2020-02-02 17:59:54  2020-6 

其他的解決辦法是罰款,只要它是可以的,我可以看到每週增長

+0

使用'df.device_create_at.dt.strftime( '%Y-%U')'或'%Y-%的W '? – Zero

回答

2

使用year + week

sheet2['device_create_week'] = sheet2['device_create_at'].dt.year.astype(str) + '-' + 
           sheet2['device_create_at'].dt.week.astype(str) 

print (sheet2) 

     device_create_at device_create_week 
136 2014-08-27 17:29:23   2014-35 
245 2015-09-06 15:46:00   2015-36 
257 2014-09-29 22:26:34   2014-40 
258 2014-11-05 13:02:18   2014-45 
480 2020-02-02 17:59:54    2020-5 
481 2020-02-02 17:59:54    2020-5 
482 2020-02-02 17:59:54    2020-5 

另一種解決方案與strftimeV

sheet2['device_create_week'] = sheet2['device_create_at'].dt.strftime('%Y-%V') 

print (sheet2) 
     device_create_at device_create_week 
136 2014-08-27 17:29:23   2014-35 
245 2015-09-06 15:46:00   2015-36 
257 2014-09-29 22:26:34   2014-40 
258 2014-11-05 13:02:18   2014-45 
480 2020-02-02 17:59:54   2020-05 
481 2020-02-02 17:59:54   2020-05 
482 2020-02-02 17:59:54   2020-05 
+0

噢,我的天,我不認爲要轉換爲字符串 –

+0

是的,但日期時間'2014-35'不存在:( – jezrael

+1

第二個對 很好''''sheet2.groupby(['device_create_week'])size() .reset_index(name ='device created count weekly')' –