Search code examples
pythonpandasnumpystatsmodelsarima

How to convert Columns into datetime index


Please refer the image in the link to take a look at my dataset. https://i.sstatic.net/0LJQP.png

I am trying to convert these columns into datetime index and country name from first column into column names. I have tried using transpose, pivot but nothing is working. I actually want to apply ARIMA model to it and as it does not support axis=1 argument, so I have only one options is to change column names into datetime index. Please help, if you have any solution to solve this problem.


Solution

  • import pandas as pd
    df = pd.DataFrame({'Country/Region': ['Afghanistan', 'Albania', 'Algeria'],
                       '1/22/20': [1, 0, 0],
                       '1/23/20': [0, 2, 3],
                       '1/24/20': [0, 1, 2]})
    df = df.T
    country = df1.iloc[0].to_list()
    df.columns = country
    df = df.drop('Country/Region')
    print(df)
    

    The Output:

            Afghanistan Albania Algeria
    1/22/20           1       0       0
    1/23/20           0       2       3
    1/24/20           0       1       2