我有一個數據框,其中列的名稱是字符串形式的日期(年 - 月)。我怎樣才能以日期時間格式轉換這些名稱? 我試着這樣做:將數據幀的列名從字符串格式更改爲datetime
new_cols = pd.to_datetime(df.columns)
df = df[new_cols]
,但我得到的錯誤:
KeyError: "DatetimeIndex(
['2000-01-01', '2000-02-01',
'2000-03-01', '2000-04-01',
'2000-05-01', '2000-06-01',
'2000-07-01', '2000-08-01',
'2000-09-01', '2000-10-01',
'2015-11-01', '2015-12-01',
'2016-01-01', '2016-02-01',
'2016-03-01', '2016-04-01',
'2016-05-01', '2016-06-01',
'2016-07-01', '2016-08-01'],
dtype='datetime64[ns]', length=200, freq=None) not in index"
謝謝!