【问题标题】:How can I can convert a timestamp column format from [YYYY/MM/DD] to from [DD/MM/YYYY] in Python data frame?如何在 Python 数据框中将时间戳列格式从 [YYYY/MM/DD] 转换为 [DD/MM/YYYY]?
【发布时间】:2021-03-08 21:12:47
【问题描述】:

在 Python 数据框中,有没有办法可以将时间戳列与格式为 [YYYY/MM/DD HH:MM:SS] 的对象转换为 [DD/MM /YYYY HH:MM:SS] 代替?

例如[2020/01/03 13:00:00] 到 [03/01/2020 13:00:00]

其中(YYYY 是年),(MM 是月),(DD 是日),(HH 是小时),(MM 是分钟),(SS 是秒)。

提前谢谢你!

【问题讨论】:

标签: python dataframe date timestamp


【解决方案1】:

这是您所期望的示例:

import datetime as dt
# Example of df with string for the date
df = pd.DataFrame({'date':['2020/01/03 13:00:00']})
# transforming it in a datetime with a specific format
df['date'] = pd.to_datetime(df.date,format='%Y/%m/%d %H:%M:%S')
#changing the format as you don't wish
df['colum1'] = df.date.dt.strftime('%Y/%m/%d %H:%M:%S')
#changing the format as you wish
df['colum2'] = df.date.dt.strftime('%d/%m/%Y %H:%M:%S')
df

输出:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-21
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多