I have a large multi-indexed data frame with many rows and columns. I want to add new columns, with names based on the original level one column names. I want to keep the level two format of the multi-index for each newly created column. The new columns calculate changes and percent changes between columns. Ideally I'd like this to be done automatically so I don't have to create new columns and column names manually.
Original:
import numpy as np
import pandas as pd
data = [[99,3,12,4,63,55]]
cols = pd.MultiIndex.from_product([['1. FY21','2. FY22','3. FY23'],['Values','Sites']])
df = pd.DataFrame(data, columns = cols)
print(df)
Desired Output:
data_new = [[99,3,-36,52,-36,1733,12,4,51,51,425,1275,63,55]]
cols_new = pd.MultiIndex.from_product([['1. FY21','FY23-FY21','FY23-FY21_ % Change','2. FY22','FY23-FY22','FY23-FY22_ % Change','3. FY23'],['Values','Sites']])
df_new = pd.DataFrame(data_new, columns = cols_new)
print(df_new)
Try:
Prints: