1

Inside my Pandas dataframe, my 'Date' columns have strings of the format mm/dd/yyyy. What I want to do is convert them into YYYY-dd-mm.

df = pd.read_csv(csv_file, header=1)

date_list = df['Date'].values
for item in date_list:
    item = pd.to_datetime(item).strftime('%Y-%m-%d')
    print(item)

This code converts the dates successfully, but the dataframe is not changed. How can I make it change the dataframe with the new format?

1 Answer 1

2

Better here is use to_datetime with all column values and then Series.dt.strftime with change format to %Y-%d-%m:

df['Date'] = pd.to_datetime(df['Date']).dt.strftime('%Y-%d-%m')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.