請問
以下datafram "CREATEDATETIME" 這欄位的時間數字要怎麼切換成日期?
ID gender birthday DXICD1 DXNME DXICD10 CREATEDATETIME
1 M 19287 571.8 Fatty liver K76.0 20201016095729.00
2 M 19287 571.8 Fatty liver K76.0 20210201112252.00
3 M 19287 571.8 Fatty liver K76.0 20210308121414.00
4 M 19287 571.8 Fatty liver K76.0 20191111101434.00
5 M 19287 571.8 Fatty liver K76.0 20200717105557.00
我猜你要的是這樣吧20200717105557.00 = > 2020/07/17 10:55:57.00
資料格式很固定,直接用字串處理一下就好,網站是我隨便找的教學
https://www.learncodewithmike.com/2019/11/python4-python.html
你可以用 python 內置的 datetime
from datetime import datetime
date_str = "20201016095729.00"
date_time_obj = datetime.strptime(date_str, "%Y%m%d%H%M%S.%f")
print(date_time_obj, type(date_time_obj))
# 2020-10-16 09:57:29 <class 'datetime.datetime'>
for string_format in ["%Y-%m-%d %H:%M:%S", "%Y/%m/%d %H:%M:%S", "%Y-%m-%d %H:%M:%S.%f"]:
print(date_time_obj.strftime(string_format))
# 2020-10-16 09:57:29 `str(date_time_obj)` 和這個是一樣的
# 2020/10/16 09:57:29
# 2020-10-16 09:57:29.000000
可以用moment套件,他們參考Moment.js寫的,之前寫前端也幾乎用Moment.js去格式化
附上github連結 https://github.com/zachwill/moment