如何仅更改 Pandas 中某些行的日期时间格式?
我有一个数据框,其中有一些列。其中之一是日期,但有些日期的格式为 dd-MMM-YY(例如:03-May-2022),有些日期的格式为 dd-mm-yy(例如:03-05-2022)。如何将列中的所有日期更改为一种格式(例如:dd-mm-yy)?
初始数据框:
序列 | 日期 |
---|---|
0 | 23-05-2022 |
1 | 14-Mar-2022 |
2 | 29-02-2020 |
所需输出:
序列 | 日期 |
---|---|
0 | 23-05-2022 |
1 | 14 -03-2022 |
2 | 29-02-2020 |
I have a dataframe in which there are some columns. One of them is Date but some dates are in the format of dd-MMM-YY (eg: 03-May-2022) and some are in the format dd-mm-yy(eg: 03-05-2022). How do I change all the dates in the column to one format(eg: dd-mm-yy)?
Initial Dataframe:
Serial | Date |
---|---|
0 | 23-05-2022 |
1 | 14-Mar-2022 |
2 | 29-02-2020 |
Required Output:
Serial | Date |
---|---|
0 | 23-05-2022 |
1 | 14-03-2022 |
2 | 29-02-2020 |
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我可以想到一种解决方案,根据日期字符串的长度处理两种不同的情况:
I can think of one solution where you handle the two different cases based on length of the date string:
您可以尝试使用自动日期解析:
或者使用
str.replace
:输出:
You can try to use automatic date parsing:
Or, using
str.replace
:Output: