[英]How can I can convert a timestamp column format from [YYYY/MM/DD] to from [DD/MM/YYYY] in Python data frame?
In a Python data frame, is there a way I can convert a timestamp column with objects formatted as [ YYYY/MM/DD HH:MM:SS] to [ DD/MM/YYYY HH:MM:SS] instead?在 Python 数据帧中,有没有办法可以将时间戳列转换为 [ YYYY/MM/DD HH:MM:SS] 格式为 [ DD/MM/YYYY HH:MM:SS] 的对象?
Eg [2020/01/03 13:00:00] to [03/01/2020 13:00:00]例如 [2020/01/03 13:00:00] 到 [03/01/2020 13:00:00]
Where (YYYY is year), (MM is month), (DD is day), (HH is hour), (MM is mins), (SS is seconds).其中(YYYY 是年),(MM 是月),(DD 是日),(HH 是小时),(MM 是分钟),(SS 是秒)。
Thank you in advance!先感谢您!
Here is an example of what you expect:这是您期望的示例:
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
OUTPUT: OUTPUT:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.