I have a dataframe that is 1010 x 1625 long consisting of open and close prices for stocks in the S&P500 Index dating back to 2015 (example below).
I am attempting to create a new dataframe based on the below, which contains daily % returns for each stock.
(current dataframe)
| | A EQUITY PX_OPEN | A EQUITY PX_LAST | AAL EQUITY PX_OPEN | AAL EQUITY PX_LAST |
| ---------- | -----------------| ---------------- | ------------------ | ------------------ |
| 02/01/2015 | 41.18 | 40.56 | 54.16 | 53.91 |
| 03/01/2015 | 40.32 | 39.80 | 54.35 | 53.88 |
| 04/01/2015 | 39.81 | 39.18 | 54.27 | 53.04 |
(desired output)
| | A EQUITY PER_RET | AAL EQUITY PER_RET |
| ---------- | -----------------| ------------------ |
| 02/01/2015 | -1.51 | -.46 |
| 03/01/2015 | -1.29 | -.87 |
| 04/01/2015 | -1.58 | -2.27 |
Formula = ((px_last - px_open) / px_open) * 100
A EQUITY_RET = ((40.56 - 41.18) / 41.18) * 100 = -1.51
My main issue is that each column header has a different name, and I am unsure sure how to loop through each of the pairs of Open/Close columns to calculate the percentage return.
Appreciate if anyone can point me in the right direction.
Thanks.