I have a DataFrame with a multiindex as follows:
df:
open close
date Symbol
2022-01-01 SPY 100 102
TSLA 232 245
2022-01-02 SPY 103 100
TSLA 222 220
AAPL 143 147
I want to convert this into a DataFrame with hierarchical columns and add another column df['delta']=df['open']-df['close'] as follows:
df2:
SPY TSLA AAPL
Open Close Open Close Open Close
date
2022-01-01 100 102 232 245 nan nan nan
2022-01-02 103 100 222 220 143 147 -4
EDIT: After I get the shape in df2, I want to calculate a third column called delta to get the following:
df:
SPY TSLA AAPL
Open Close delta Open Close delta Open Close delta
date
2022-01-01 100 102 -2 232 245 -13 nan nan nan
2022-01-02 103 100 3 222 220 2 143 147 -4
How can this be done? I tried pivoting the DataFrame but it did not work.