0

I am trying to copy a multiindex dataframe to another dataframe, however, it is not working somehow. Here, is the toy example.

arrays = [np.array(['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux']), \
          np.array(['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two'])]
df = pd.DataFrame(np.random.randn(8, 4), index=arrays)

mindx = df.index.set_names(['X', 'Y'])
df = df.set_index(mindx)


             0          1            2         3
X   Y               
bar one -2.327132   -0.119892   -0.288562   0.767718
    two -0.056876   -0.105477   1.012807    -0.088878
baz one 1.661728    0.924212    -0.618278   0.119823
    two 1.131492    -0.857924   0.711160    -0.029915
foo one 0.895299    -0.605197   0.227541    1.892720
    two -0.203198   0.507463    -1.573138   1.052957
qux one -0.751430   1.871923    0.359925    -2.157044
    two -0.924937   -1.830958   0.934699    0.909292

Now, I want to assign this dataframe to another multiindex dataframe which is initialised this way:

my_index = pd.MultiIndex(levels=[[],['one', 'two']],\
                     labels=[[],[]],\
                     names=[u'X', u'Y'])
df_mult = pd.DataFrame(index=my_index, columns=df.columns)

To copy, I am using the following command which is doing nothing. I am unable to understand why this is happening?

df_mult.loc[pd.IndexSlice[:, 'one'], :] = df.copy()

Any help will be appreciated. Thanks in advance.

Milan Jain
  • 459
  • 7
  • 17

0 Answers0