在python中将数据集的日期转换为yyyy-mm-dd



我已经编写了代码,除了日期之外,我的结果很好。我的日期格式为"2020年1月12日",我想将其转换为yyyy-mm-dd。有人能帮我纠正我正在犯的错误吗。这是我的代码:


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

输出为:

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'

我需要执行什么来纠正我的错误?

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

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

最新更新