如何在 pandas 中将多种日期格式转换为一种格式 [英] how to convert multiple date formats in one format in pandas

查看:68
本文介绍了如何在 pandas 中将多种日期格式转换为一种格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下熊猫数据框,日期列为object

I have following pandas dataframe with date column as object

   ID      Date                  Volume
   0       13-02-2018 00:06       85
   1       13-02-2018 00:10       70
   2       13-02-2018 00:11       100
   3       2018-02-13 06:30       123
   4       02-13-2018 07:56       100

我想将其转换为以下一种格式

I want to convert it to following one format

   ID      Date                  Volume
   0       2018-02-13 00:06       85
   1       2018-02-13 00:10       70
   2       2018-02-13 00:11       100
   3       2018-02-13 06:30       123
   4       2018-02-13 07:56       100

我正在尝试通过以下命令来实现这一目标

I am trying to achieve this by following command

df['Date'] = df.date.apply(lambda x: pd.to_datetime(x).strftime('%Y-%m-%d %H:%M')[0])

但是它抛出一个错误.我该如何在大熊猫中做到这一点?

But it throws an error. How can I do it in pandas?

推荐答案

尝试一下:

df['Date'] = pd.to_datetime(df.Date)

df['Date'] = df['Date'].dt.strftime('%Y-%m-%d %H:%M')

链接: Series.dt.strftime

这篇关于如何在 pandas 中将多种日期格式转换为一种格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆