繁体   English   中英

如何使用 pandas 更改 python 中的日期格式

[英]How to change date format in python with pandas

我正在处理 pandas 中的大数据,我的日期格式有问题,这是一列的格式

Wed Feb 24 12:06:14 +0000 2021

我认为用这样的格式更改所有列的格式更容易

'%d/%m/%Y, %H:%M:%S'

我怎样才能做到这一点?

这对你有用吗?

pandas.to_datetime(s, format='%d/%m/%Y, %H:%M:%S')   

资料来源: https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior

您可以将以下 function 用于您的数据集。

def change_format(x):
format = dt.datetime.strptime(x, "%a %b %d %H:%M:%S %z  %Y")
new_format = format.strftime('%d/%m/%Y, %H:%M:%S')
return new_format

然后使用df['date_column'] = df['date_column'].apply(change_format)它。 这里df是您的数据集。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM