pandas - change multiple date time formats to single format in pandas dataframe
问题描述
I have a DataFrame with multiple formats as shown below
0 07-04-2021
1 06-03-1991
2 12-10-2020
3 07/04/2021
4 05/12/1996
What I want is to have one format after applying the Pandas function to the entire column so that all the dates are in the format
date/month/year
What I tried is the following
date1 = pd.to_datetime(df['Date_Reported'], errors='coerce', format='%d/%m/%Y')
But it is not working out. Can this be done? Thank you
解决方案
try with dayfirst=True
:
date1=pd.to_datetime(df['Date_Reported'], errors='coerce',dayfirst=True)
output of date1
:
0 2021-04-07
1 1991-03-06
2 2020-10-12
3 2021-04-07
4 1996-12-05
Name: Date_Reported, dtype: datetime64[ns]
If needed:
date1=date1.dt.strftime('%d/%m/%Y')
output of date1
:
0 07/04/2021
1 06/03/1991
2 12/10/2020
3 07/04/2021
4 05/12/1996
Name: Date_Reported, dtype: object
推荐阅读
- sed - 用 sed 替换内联日期格式
- javascript - momentjs - 更改当地时间的格式
- javascript - 视频 HTML5 中的自定义擦洗栏,设置 currentTime 不起作用
- android - Espresso 测试无法在某些设备上单击 DialogButton
- android - 从通知中打开当前正在运行的子活动,而不是创建新活动
- python - Python win32com模块在外部应用程序中保存命令
- java - “fieldType”:具有完全限定名称的“Sig”不以“signature”为前缀创建 java.lang.ClassCastException
- python - 我可以在 Vue 中使用 Django 身份验证吗
- perforce - 如何在 Perforce 中创建一个新的 Shelve Changelist
- javascript - 导出函数会加载整个文件