当前位置: 动力学知识库 > 问答 > 编程问答 >

python - How to Format a Date Column in Pandas?

问题描述:

I have a dataframe df that look like this:

 ID Date

0 1 2008-01-24

1 2 2007-02-17

The format of Date is %Y-%m-%d

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?

网友答案:

Use the .dt accessor:

df["Date"] = df["Date"].dt.strftime("%m-%d-%Y")
分享给朋友:
您可能感兴趣的文章:
随机阅读: