MEhsan MEhsan - 4 months ago 11
Python Question

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?

Answer

Use the .dt accessor:

df["Date"] = df["Date"].dt.strftime("%m-%d-%Y")