Change dataframe column names from string format to datetime

前端 未结 2 1236
伪装坚强ぢ
伪装坚强ぢ 2021-02-06 16:16

I have a dataframe where the names of the columns are dates (Year-month) in the form of strings. How can I convert these names in datetime format? I tried doing this:

         


        
2条回答
  •  悲哀的现实
    2021-02-06 16:58

    As an expansion to jezrael's answer, the original code will be trying to slice the df array by the array stored in new_cols and store the result as df - but since those values don't exist in df yet it returns an error saying it can't find that index to slice by.

    As such you need to declare that you're changing the name of the columns, as in jezrael's answer.

提交回复
热议问题