I need advanced python pandas help for my problem. I have such a dataframe which contains 4 string columns that identify the hierarchy of the item and the rest are columns that follow the following pattern:
Text | Period (example: "ACTUAL | 2020-08-01")
Text can be either: Actual, Forecast t-1 or Forecast t-3. Period can be any month-year: 2020-08-01 .... For example: "ACTUAL | 2020-08-01" "FORECAST T-1 | 2020-08-01" "FORECAST T-3 | 2020-08-01"
df.columns
show as:
['HIERARCHY 1', 'HIERARCHY 2', 'HIERARCHY 3',
'ITEM NUMBER', 'ACTUAL|2020-06-01', 'ACTUAL|2020-07-01',
'ACTUAL|2020-08-01', 'ACTUAL|2020-09-01', 'ACTUAL|2020-10-01',
'ACTUAL|2020-11-01', 'ACTUAL|2020-12-01', 'ACTUAL|2021-01-01',
'ACTUAL|2021-02-01', 'ACTUAL|2021-03-01', 'ACTUAL|2021-04-01',
'ACTUAL|2021-05-01', 'ACTUAL|2021-06-01', 'ACTUAL|2021-07-01',
'ACTUAL|2021-08-01', 'ACTUAL|2021-09-01', 'ACTUAL|2021-10-01',
'FORECAST T-1|2021-01-01', 'FORECAST T-1|2021-08-01',
'FORECAST T-1|2021-09-01', 'FORECAST T-1|2021-10-01',
'FORECAST T-1|2021-11-01', 'FORECAST T-3|2021-03-01',
'FORECAST T-3|2021-10-01', 'FORECAST T-3|2021-11-01',
'FORECAST T-3|2021-12-01', 'FORECAST T-3|2022-01-01',
'FORECAST T-1|2020-07-01', 'FORECAST T-1|2020-08-01',
'FORECAST T-1|2020-09-01', 'FORECAST T-1|2020-10-01',
'FORECAST T-1|2020-11-01', 'FORECAST T-1|2020-12-01',
'FORECAST T-1|2021-02-01', 'FORECAST T-1|2021-03-01',
'FORECAST T-1|2021-04-01', 'FORECAST T-1|2021-05-01',
'FORECAST T-1|2021-06-01', 'FORECAST T-3|2020-09-01',
'FORECAST T-3|2020-10-01', 'FORECAST T-3|2020-11-01',
'FORECAST T-3|2020-12-01', 'FORECAST T-3|2021-01-01',
'FORECAST T-3|2021-02-01', 'FORECAST T-3|2021-04-01',
'FORECAST T-3|2021-05-01', 'FORECAST T-3|2021-06-01',
'FORECAST T-3|2021-07-01', 'FORECAST T-3|2021-08-01'],
For any period we can have either: Actual, Forecast t-1 or Forecast t-3. There is no guarantee that there are the three of them present. Maybe only one of them, for example for specific period we can have only: "ACTUAL | 2020-08-01", "FORECAST T-1 | 2020-08-01", but there is no column showing: "FORECAST T-3 | 2020-08-01".
My goal is to achieve the following dataframe:

First step 1:
Re-order the column names as follows:
df.columns
should be: ACTUAL | Period 1 .... ACTUAL | Period N, where N is the last period Followed by FORECAST T-1 | Period 1 .... FORECAST T-1 | Period N, where N is the last period Lastly, FORECAST T-3 | Period 1 .... FORECAST T-3 | Period N, where N is the last period
It should be ordered by: Actual, Forecast t-1 and lastly Forecast t-3. Within each category, it should be from oldest period to most recent (left to right).
Step 2:
I want to perform operations for columns that belong to the same period. I want to loop through each of the periods within the column names, and for each period, I want to see if there is column: "ACTUAL | Any Period (ex. 2020-08-01)" and Forecast metrics for that periods, if there are: then I want to compute the absolute error, which would be computed as:
df["ABS ERROR T-1 | 2020-08-01"] = (df["ACTUAL | 2020-08-01"] - df["FORECAST T-1 | 2020-08-01"]).abs()
df["ABS ERROR T-3 | 2020-08-01"] = (df["ACTUAL | 2020-08-01"] - df["FORECAST T-3 | 2020-08-01"]).abs()
If there is no ACTUAL in the current period, then skip. If there is no FORECAST (either T-1 or T-3), then skip the period as well.
If there is at least ACTUAL and 1 Forecast period then compute the Absolute Error.
Then, put it on the dataframe at the end as a new column. I have more than 50 columns. I cannot do this manually so I need to find a pattern for my problem.

