正在更新熊猫数据帧中的日期格式



我正在尝试使用panda更改df中的日期格式。我当前的代码如下:

data = pd.read_csv('pub?gid=31644116&single=true&output=csv', 
usecols=[0,1,2], 
header=0,
encoding="utf-8-sig",
index_col='Day Index')

data['Revenue'] = data['Revenue'].str.replace(',','').str.replace('£','').astype('float')
data['E-commerce Conversion Rate'] = data['E-commerce Conversion Rate'].str.replace('%','').astype('float')
data.apply(pd.to_numeric)
print("we have a total of:", len(data), " samples")
data.head()

并返回:

we have a total of: 109  samples
Revenue E-commerce Conversion Rate
Day Index       
01/05/2020  4396.89 0.99
02/05/2020  7117.02 1.60
03/05/2020  3248.22 1.04
04/05/2020  8843.80 1.93
05/05/2020  5863.42 1.54

我想把日期格式从年月日更新为年月日。当我添加以下行时,我得到以下错误:

data['Day Index']=pd.to_datetime(data['Day Index'].astype(str), format='%y-%m-%d')
KeyError: 'Day Index'
During handling of the above exception, another exception occurred:

问题是因为我正在设置";日指数";列作为index_col?任何帮助都将不胜感激!

如果需要转换索引值,请使用data.index并更改匹配DD/MM/YYYY:的日期时间格式

data.index=pd.to_datetime(data.index, format='%d/%m/%Y')

此外,在您的代码中没有将转换值的输出分配给数字,请使用:

data = data.apply(pd.to_numeric)

您可以尝试传递yearfirst参数而不是格式。希望这有帮助:

yearfirst=True

最新更新