如何使两个不同的日期格式在单列中唯一? [英] How to make two different date format in single column unique?

查看:77
本文介绍了如何使两个不同的日期格式在单列中唯一?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只有一列,但我不了解如何操作,但是同一列中有两种不同的格式.

I have one column in I don't understand how but there is two different format in a single column.

df['Date'] = [6/24/2019,6/14/2019,2019-09-06 00:00:00,6/14/2019,6/14/2019]

我想要对其进行进一步处理,所以我希望将其制成单一格式.

I want process it further so I want make it in a single format.

df['Date'] = [6/24/2019,6/14/2019,9/06/2019,6/14/2019,6/14/2019]

我已经尝试过类似的事情

I have tried something like this

data['New_date'] = pd.to_datetime(df['Date'], format = '%m/%d/%Y')

但这给了我这个错误

ValueError:时间数据6/24/2019与指定的格式不匹配

ValueError: time data 6/24/2019 doesn't match format specified

推荐答案

使用 to_datetime NaTerrors='coerce'(如果不匹配),并用Series. org/pandas-docs/stable/reference/api/pandas.Series.combine_first.html"rel =" nofollow noreferrer> Series.combine_first

Use to_datetime with both formats and errors='coerce' for NaT if not match and replace missing values by another Series by Series.combine_first or Series.fillna them, last convert to strings by Series.dt.strftime:

s1 = pd.to_datetime(data['Date'], format='%Y-%d-%m %H:%M:%S', errors='coerce')
s2 = pd.to_datetime(data['Date'], format = '%m/%d/%Y', errors='coerce')

#2 possible solutions
data['new'] = s1.fillna(s2).dt.strftime('%m/%d/%Y')
data['new'] = s1.combine_first(s2).dt.strftime('%m/%d/%Y')
print (data)
                  Date         new
0            6/24/2019  06/24/2019
1            6/14/2019  06/14/2019
2  2019-09-06 00:00:00  06/09/2019
3            6/14/2019  06/14/2019
4            6/14/2019  06/14/2019

这篇关于如何使两个不同的日期格式在单列中唯一?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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