0

I have a stacked dataframe that looks like this:

YEAR    
1973  1     26.18
      2     26.18
      3     25.53
...
1974  1     24.53
      2     24.89
      3     24.17
...
1975  1     24.21
      2     24.98
      3     25.47

The numbers in the index represent the month that the data was taken and I'm looking to get a dataframe that looks something like this:

Date   
1-1973     26.18
2-1973     26.18
3-1973     25.53
...
1-1974     24.53
2-1974     24.89
3-1974     24.17
...
1-1975     24.21
2-1975     24.98
3-1975     25.47

Does anyone know of a way I could pull this off?

I tried unstacking the dataframe which gave me something like this:

1   2   3   4   5   6   7   8   9   10  11  12
YEAR                                                
1973    26.18   26.18   25.53   24.52   22.63   22.35   21.10   22.45   23.34   23.47   24.12   24.75
1974    24.53   24.89   24.17   23.82   22.29   20.19   19.38   21.41   23.97   24.67   23.37   23.57
1975    24.21   24.98   25.47   22.99   24.22   20.38   19.45   20.98   22.73   25.10   24.04   23.97
1976    25.39   24.87   25.44   24.39   22.71   21.65   21.05   22.26   23.73   23.31   23.59   24.42

But this is not quite what I'm looking for.

Thanks for the help!

  • (There's a slight complication that you will run into if you use that answer. In order to get the values of a column inside a MultiIndex, you need to use `df.index.get_level_values(0)` for the year and `df.index.get_level_values(1)` for the month. See also https://stackoverflow.com/questions/56515638/access-individual-levels-of-multi-index-pandas ) – Nick ODell Apr 03 '22 at 16:10
  • Please provide enough code so others can better understand or reproduce the problem. – Community Apr 03 '22 at 16:26

0 Answers0