简体   繁体   中英

convert dates from dataset to yyyy-mm-dd in python

I have already written my codes and I am having good results except for the dates. My dates are in form 'January 12 2020' and I want to convert it to yyyy-mm-dd. Can someone please help me to correct the mistake i am doing. Here are my codes:


    Date = item['reviews']
    for d in Date:
        names.append(name)
        date = d['date'].strftime("%Y-%m-%d")
        dates.append(date)
        print('>>>>>>>>>>>>>>>>>> ', date)

the output are:

Traceback (most recent call last):
  File "C:/Users/User/Pycharmresult.py", line 95, in <module>
    date = d['date'].strftime("%Y-%m-%d")
AttributeError: 'str' object has no attribute 'strftime'

What do i need to perform to correct my error?

我认为您必须首先将您的列转换为datetime

pd.to_datetime(d['date']).strftime("%Y-%m-%d")

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