如何在Python pandas datetime Dataframe 中将 Dataframe 中的所有日期时间转换为特定格式

zvokhttg  于 2022-12-21  发布在  Python
关注(0)|答案(1)|浏览(182)

i have a dataframe with 1.000.000 there is a column called "datetimes". The problem is that the half datetimes follow this format:

2022-month-day hour-minutes-seconds

and the rest datetimes follow this format

2022-day-month hour-minutes-seconds

does anybody knows what i have to add in my code in order to have this ouput i want all the datetimes to be with this format: 2022-MONTH-DAY a sample of my dataframe is Datetime 0 2022-09-06 12:33:40 1 2022-09-06 12:33:50 2 2022-09-06 12:34:00 . . . 10.000 2022-09-06 23:59:50 . . 55251 2022-06-15 22:02:20 55252 2022-06-15 22:02:30 55253 2022-06-15 22:02:40 55254 2022-06-15 22:02:50 . . 311492 2022-07-15 13:52:20 311493 2022-07-15 13:52:30 311494 2022-07-15 13:52:40 . .
537534 2022-10-08 17:47:00 537535 2022-10-08 17:47:10 537536 2022-10-08 17:47:20
as you can see the formation of the datetime changes many times

kmpatx3s

kmpatx3s1#

因为我是个初学者,所以如果我的答案有帮助的话,我会告诉你的,但是如果你能展示你的代码,看看你是否写错了什么或者缺少了什么也会很有帮助。
我做了一个小片段:

import pandas as pd

date = pd.DataFrame({"Date": ["18-12-2022", "19-12-2022", "20-12-2022"]})

date["Date"] = pd.to_datetime(date["Date"]).dt.strftime('2022-%m-%d')

print(date)

输出:

Date
0  2022-12-18
1  2022-12-19
2  2022-12-20

希望有帮助。

相关问题