Reading a matrix data with stacked columns in pandas
Hello ,
Newbie to Python here. Need some help in reading xls file with following format as pandas dataframe and writing it as a record
xls layout looks like this
| SP500 | DJIA
| Equity| Equity
| Large | BlueChip
-----------------------------
1-Apr-23 | 3.51 | 2.56
1-Mar-23 | -2.61 | 1.89
1-Feb-23 | 6.18 | -4.06
1-Jan-23 | -5.9 | 2.83
Basically its a timeseries of performance return of 2 major indices.
I'd like to flatten it out something like this
Date | Index | AssetClass | Type | Return
1-Apr-23 | SP500 | Equities | Large | 3.51
1-Mar-23 | SP500 | Equities | Large | -2.61
1-Feb-23 | SP500 | Equities | Large | 6.18
1-Jan-23 | SP500 | Equities | Large | -5.9
1-Apr-23 | DJIA | Equities | BlueChip | 2.56
1-Mar-23 | DJIA | Equities | BlueChip | 1.89
1-Feb-23 | DJIA | Equities | BlueChip | -4.06
1-Jan-23 | DJIA | Equities | BlueChip | 2.83
For a single column header, I used dataframe.melt()
function to flatten it out. I am unsure how I could achieve it for stacked columns.
Appreciate your help