1

I have a dataframe that looks as follows:

          Sim_1     Sim_2     Sim_3          
2016   1.063708  1.008885  1.028539  
2017   1.114644  0.994331  1.043218    

In order to append it to another, two-indexed dataframe, I'd like to add a new level below the existing column names, such that the dataframe will have following structure

SIMULATION  Sim_1     Sim_2     Sim_3   
IDX          LR        LR        LR
2016      1.063708  1.008885  1.028539  
2017      1.114644  0.994331  1.043218   

How would I do this?

2 Answers 2

1

You can use MultiIndex.from_arrays:

df.columns = pd.MultiIndex.from_arrays([df.columns, 
                                       ['LR'] * len(df.columns)], 
                                       names=('SIMULATION','IDX'))
print (df)
SIMULATION     Sim_1     Sim_2     Sim_3
IDX               LR        LR        LR
2016        1.063708  1.008885  1.028539
2017        1.114644  0.994331  1.043218
Sign up to request clarification or add additional context in comments.

Comments

1

You can use pandas.MultiIndex.from_product:

>>> df.columns = pd.MultiIndex.from_product([df.columns, ['LR']], names=['SIMULATION', 'IDX'])
>>> df
SIMULATION     Sim_1     Sim_2     Sim_3
IDX               LR        LR        LR
2016        1.063708  1.008885  1.028539
2017        1.114644  0.994331  1.043218

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.