gpt4 book ai didi

python - 获取准备在 pandas 中绘制的多年的周数

转载 作者:行者123 更新时间:2023-12-01 02:32:01 25 4
gpt4 key购买 nike

这是我的数据

我正在做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

其他解决方案只要可绘制就可以,而且我每周都能看到增长

最佳答案

使用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

关于python - 获取准备在 pandas 中绘制的多年的周数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46701899/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com