简体   繁体   中英

Changing date format convert mmm-yy to yyyy/mm/dd

I have a .CSV file with a column "Date". It has the full date in it eg 1/9/2020 but is formatted to Sep-20. (All dates are the first of every month)

![CSV 的格式

The issue is that python is reading the formatted .CSV file's formatted value of Sep-20. How do I change all the values to a yyyy/mm/dd (2020/09/01) format?

What I tried so far but to no avail.

import pandas as pd
tw_df = pd.read_csv("tw_data.csv", index_col = "Date", parse_dates = True, format = "%Y%m%d")

Error Message

TypeError: parser_f() got an unexpected keyword argument 'format'

You can use datetime to convert the information to date inside Pandas. Use strptime to convert string on a given format to date format that you can work inside Pandas.

Check the code below:

import pandas as pd
from datetime import datetime

df = pd.read_csv('tw_data.csv')
conv = lambda x: datetime.strptime(x, "%b-%y")
df["Date"] = df["Date"].apply(conv)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM