multi index - Pandas: add a column to a multiindex column dataframe -


i add column second level of multiindex column dataframe.

in [151]: df out[151]:  first        bar                 baz            second       1       2       1       2        0.487880 -0.487661 -1.030176  0.100813  b       0.267913  1.918923  0.132791  0.178503 c       1.550526 -0.312235 -1.177689 -0.081596  

the usual trick of direct assignment not work:

in [152]: df['bar']['three'] = [0, 1, 2]  in [153]: df out[153]:  first        bar                 baz            second       1       2       1       2        0.487880 -0.487661 -1.030176  0.100813 b       0.267913  1.918923  0.132791  0.178503 c       1.550526 -0.312235 -1.177689 -0.081596 

how can add third row under "bar"?

it's pretty simple (fwiw, thought way):

df['bar', 'three'] = [0, 1, 2] df = df.sort_index(axis=1) print(df)          bar                        baz                   1       2  3       1       2 -0.212901  0.503615      0 -1.660945  0.446778 b -0.803926 -0.417570      1 -0.336827  0.989343 c  3.400885 -0.214245      2  0.895745  1.011671 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -