2

I currently have a dataframe that looks like this

CODE      DATE        VALUE
0001   2020-10-09      100
0001   2020-10-10      102
0002   2020-10-09      50
0002   2020-10-10      48
0003   2020-10-09      75
0003   2020-10-10     77.5
0004   2020-10-09      60
0004   2020-10-10      62
...

Unstacking the initial dataframe with the below code

df = df.set_index(['DATE', 'CODE']).unstack(1)
df.columns = df.columns.droplevel()

will creates a dataframe like this

   DATE        0001     0002     0003     0004
2020-10-09      100      50       75       60
2020-10-10      102      48      77.5      62
...

My question is, is there a way in which I can set the order of the columns so that it looks like this

   DATE        0002     0001     0004     0003
2020-10-09      50       100       60      75
2020-10-10      48       102       62     77.5
    ...

Or Any other order in which I can specify.

The only method I can really think of is to create a new dataframe and manually add the columns which is something that I want to avoid.

kli4
  • 33
  • 3

0 Answers0