I have a DataFrame df that contains price data (Open, Close, High, Low) for every day in the time from January 2010 to December 2021:
Name | ISIN | Data | 02.01.2010 | 05.01.2010 | 06.01.2010 | ... | 31.12.2021 |
---|---|---|---|---|---|---|---|
Apple | US9835635986 | Price Open | 12.45 | 13.45 | 12.48 | ... | 54.12 |
Apple | US9835635986 | Price Close | 12.58 | 15.35 | 12.38 | ... | 54.43 |
Apple | US9835635986 | Price High | 12.78 | 15.85 | 12.83 | ... | 54.91 |
Apple | US9835635986 | Price Low | 12.18 | 13.35 | 12.21 | ... | 53.98 |
Microsoft | US1223928384 | Price Open | 12.45 | 13.45 | 12.48 | ... | 43.56 |
... | .. | ... | ... | ... | ... | ... | ... |
I am trying to reshape the table into the format below:
Date | Name | ISIN | Price Open | Price Close | Price High | Price Low | |
---|---|---|---|---|---|---|---|
02.01.2010 | Apple | US9835635986 | 12.45 | 12.58 | 12.78 | 12.18 | |
05.01.2010 | Apple | US9835635986 | 13.45 | 15.35 | 15.85 | 13.35 | |
... | ... | ... | ... | ... | ... | ... | ... |
02.01.2010 | Microsoft | US1223928384 | 12.45 | 13.67 | 13.74 | 12.35 |
Simply transposing the DateFrame did not work. I also tried pivot which gave the error message that the operands ould not be broadcasted to different shapes.
dates = ['NAME','ISIN']
dates.append(df.columns.tolist()[3:]) # appends all columns names starting with 02.01.2010
df.pivot(index = dates, columns = 'Data', Values = 'Data')
How can I get this DataFrame in the desired format?