如何格式化 pandas 中的日期列? [英] How to Format a Date Column in Pandas?
本文介绍了如何格式化 pandas 中的日期列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数据帧df
,如下所示:
I have a dataframe df
that look like this:
ID Date
0 1 2008-01-24
1 2 2007-02-17
Date
的格式为%Y-%m-%d
如何将日期格式化为%m-%d-%Y
格式?
How can I format the dates to %m-%d-%Y
format?
我尝试使用此语法,但是它没有提供正确的格式:
I tried using this syntax but it did not give the right format:
df["Date"] = df["Date"].strftime("%m-%d-%Y")
有什么办法解决这个问题吗?
Any idea how to solve this?
推荐答案
使用 .dt
访问器:
Use the .dt
accessor:
df["Date"] = df["Date"].dt.strftime("%m-%d-%Y")
这篇关于如何格式化 pandas 中的日期列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文