Rex
Rex

Reputation: 2147

How to do join of multiindex dataframe with another multiindex dataframe?

This is to go further from the following thread: How to do join of multiindex dataframe with a single index dataframe?

The multi-indices of df1 are sublevel indices of df2.

In [1]: import pandas as pd
In [2]: import numpy as np
In [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)
In [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)
In [11]: sngl
Out[11]: 
        one       two     three      four
a  2.946876 -0.751171  2.306766  0.323146
b  0.192558  0.928031  1.230475 -0.256739

In [12]: mult
Out[12]: 
        one  two three four
10 1 a  NaN  NaN   NaN  NaN
     b  NaN  NaN   NaN  NaN
   2 a  NaN  NaN   NaN  NaN
     b  NaN  NaN   NaN  NaN
20 1 a  NaN  NaN   NaN  NaN
     b  NaN  NaN   NaN  NaN
   2 a  NaN  NaN   NaN  NaN
     b  NaN  NaN   NaN  NaN


In [13]: mult.ix[(10,1)] = sngl

In [14]: mult
Out[14]: 
        one  two three four
10 1 a  NaN  NaN   NaN  NaN
     b  NaN  NaN   NaN  NaN
   2 a  NaN  NaN   NaN  NaN
     b  NaN  NaN   NaN  NaN
20 1 a  NaN  NaN   NaN  NaN
     b  NaN  NaN   NaN  NaN
   2 a  NaN  NaN   NaN  NaN
     b  NaN  NaN   NaN  NaN

# the new dataframes
sng2=pd.concat([sng1,sng1],keys=['X','Y'])
mult2=pd.concat([mult,mult],keys=['X','Y'])

In [110]:

sng2
Out[110]:
             one     two           three    four
X   a   0.206810    -1.056264   -0.572809   -0.314475
    b   0.514873    -0.941380   0.132694    -0.682903
Y   a   0.206810    -1.056264   -0.572809   -0.314475
    b   0.514873    -0.941380   0.132694    -0.682903

In [121]: mult2
Out[121]:
               one  two three   four
X   10  1   a   NaN NaN NaN NaN
            b   NaN NaN NaN NaN
        2   a   NaN NaN NaN NaN
            b   NaN NaN NaN NaN
    20  1   a   NaN NaN NaN NaN
            b   NaN NaN NaN NaN
        2   a   NaN NaN NaN NaN
            b   NaN NaN NaN NaN
Y   10  1   a   NaN NaN NaN NaN
            b   NaN NaN NaN NaN
        2   a   NaN NaN NaN NaN
            b   NaN NaN NaN NaN
    20  1   a   NaN NaN NaN NaN
            b   NaN NaN NaN NaN
        2   a   NaN NaN NaN NaN
            b   NaN NaN NaN NaN

the code above is long, please scroll The two multilevel indices of sng2 share the 1st and 4th indices of mul2. ('X','a') for example.

@DSM proposed a solution to work with a multiindex df2 and single index df1

mult[:] = sngl.loc[mult.index.get_level_values(2)].values

BUt DataFrame.index.get_level_values(2) can only work for one level of index.

Upvotes: 0

Views: 535

Answers (1)

JoeCondron
JoeCondron

Reputation: 8906

It's not clear from the question which index levels the data frames share. I think you need to revise the set-up code as it gives an error at the definition of sngl. Anyway, suppose mult shares the first and second level with sngl you can just drop the second level from the index of mult and index in:

mult[:] = sngl.loc[mult.index.droplevel(2)].values

On a side note, you can construct a multi index from a product directly using pd.MultiIndex.from_product rather than using itertools

Upvotes: 1

Related Questions