Search code examples
pythonpandasdataframehierarchical-datamulti-index

change pandas MultiIndex level by '+1'


How to rename MultiIndex level = 0 as mentioned in this_df like 'ti'->'ti+1'?

this_df contains normalized weights using ti data for computing weighted sums in ti+1.

this_df = pd.read_csv('http://www.boomboxbooking.de/stackq/data.csv', index_col =[0,1], sep=';')

new_index_level_0 = ['t1','t2','t3']

Solution

  • Use set_levels on the Index object to assign new values:

    In[72]:
    this_df.index = this_df.index.set_levels(['t1','t2','t3'], 0)
    this_df
    
    Out[72]: 
                   earnings  costs
       Unnamed: 1                 
    t1 S1              0.50   0.50
       S2              0.50   0.50
    t2 S1              0.20   0.09
       S2              0.80   0.91
    t3 S1              0.43   0.20
       S2              0.57   0.80