python - 我在数据框中有一个日期列。我想在该列中更改日期的格式
问题描述
我在数据集中有一个日期列,其中日期类似于“Apr-12”、“Jan-12”格式。我想将格式更改为 04-2012,01-2012。我正在寻找可以做到这一点的功能。
解决方案
我想我认识一个同名的人。这里的笑话是解决您问题的方法。我们确实有一个名为strptime()的内置函数,因此它会占用字符串,然后转换为您想要的格式。
- 您需要先导入 datetime,因为它是 python 的 datetime 包的一部分。不需要安装任何东西,只需导入它。
- 然后这样工作:
datetime.strptime(your_string, format_you_want)
# You can also do this, from datetime import * (this imports all the functions of datetime)
from datetime import datetime
str = 'Apr-12'
date_object = datetime.strptime(str, '%m-%Y')
print(date_object)
我希望这对你有用。快乐编码:)
推荐阅读
- javascript - 使用 nodejs 在 JSON 中显示一对多结果
- python-3.x - 如何使用 python 请求登录到浏览器提示符(下图)?
- javascript - 如何在 Javascript 中增加 4 个小数位的值?
- docker - 设备 ACL 在 docker 容器中不可见
- dialogflow-es - 我从 Dialogflow 中删除了代理,但它仍然存在
- swiftui - SwiftUI 如果数字大于 30
- matlab - 两个子图,一个非线性,同步问题
- r - XG-Boost: predict(...,predleaf = T) - 结果是什么意思?
- python - 如何修改“if 条件”以便同时将其应用于不同的列表?
- java - Selenium 随机点击到 Div 的