You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
IIn [3]: import itertools
In [4]: inner = ('a','b')
In [5]: outer = ((10,20), (1,2))
In [6]: cols = ('one','two','three','four')
In [7]: sngl = pd.DataFrame(np.random.randn(2,4), index=inner, columns=cols)
IIn [8]: index_tups = list(itertools.product(*(outer + (inner,))))
In [9]: index_mult = pd.MultiIndex.from_tuples(index_tups)
In [10]: mult = pd.DataFrame(index=index_mult, columns=cols)
and the solution
In [48]: nm = mult.reset_index().set_index('level_2')
In [49]: nm
Out[49]:
level_0 level_1 one two three four
level_2
a 10 1 NaN NaN NaN NaN
b 10 1 NaN NaN NaN NaN
a 10 2 NaN NaN NaN NaN
b 10 2 NaN NaN NaN NaN
a 20 1 NaN NaN NaN NaN
b 20 1 NaN NaN NaN NaN
a 20 2 NaN NaN NaN NaN
b 20 2 NaN NaN NaN NaN
A bug here????
This should probably work with a series on the rhs; this might be a buglet
In [50]: nm.loc['a',sngl.columns] = sngl.loc['a'].values
In [51]: nm
Out[51]:
level_0 level_1 one two three four
level_2
a 10 1 0.3738456 -0.2261926 -1.205177 0.08448757
b 10 1 NaN NaN NaN NaN
a 10 2 0.3738456 -0.2261926 -1.205177 0.08448757
b 10 2 NaN NaN NaN NaN
a 20 1 0.3738456 -0.2261926 -1.205177 0.08448757
b 20 1 NaN NaN NaN NaN
a 20 2 0.3738456 -0.2261926 -1.205177 0.08448757
b 20 2 NaN NaN NaN NaN
In [52]: nm.reset_index().set_index(['level_0','level_1','level_2'])
Out[52]:
one two three four
level_0 level_1 level_2
10 1 a 0.3738456 -0.2261926 -1.205177 0.08448757
b NaN NaN NaN NaN
2 a 0.3738456 -0.2261926 -1.205177 0.08448757
b NaN NaN NaN NaN
20 1 a 0.3738456 -0.2261926 -1.205177 0.08448757
b NaN NaN NaN NaN
2 a 0.3738456 -0.2261926 -1.205177 0.08448757
b NaN NaN NaN NaN
The text was updated successfully, but these errors were encountered:
http://stackoverflow.com/questions/17451843/copying-a-single-index-dataframe-into-a-multiindex-dataframe/17452237#17452237
and the solution
A bug here????
This should probably work with a series on the rhs; this might be a buglet
The text was updated successfully, but these errors were encountered: