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?
更好的方法是将to_datetime
与所有列值一起使用,然后将Series.dt.strftime
与更改格式更改为%Y-%d-%m
:
df['Date'] = pd.to_datetime(df['Date']).dt.strftime('%Y-%d-%m')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.