我不了解其中的一栏,但是同一栏中有两种不同的格式。
df['Date'] = [6/24/2019,6/14/2019,2019-09-06 00:00:00,6/14/2019,6/14/2019]
我想对其进行进一步处理,所以我希望以单一格式进行处理。
df['Date'] = [6/24/2019,6/14/2019,9/06/2019,6/14/2019,6/14/2019]
我已经尝试过这样的事情
data['New_date'] = pd.to_datetime(df['Date'], format = '%m/%d/%Y')
但这给了我这个错误
ValueError:时间数据6/24/2019与指定的格式不匹配
使用to_datetime两种格式和errors='coerce'用于NaT如果没有被其他的匹配和替换缺失值Series的Series.combine_first或Series.fillna他们,最后转换为由字符串Series.dt.strftime:
to_datetime
errors='coerce'
NaT
Series
Series.combine_first
Series.fillna
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