Q:

How to combine multi-index data frames on certain index

I have a list of multi-index data frames, for example:

df1 = pd. DataFrame(np.random.randn(4, 4), 
                      index=[np.array(['bar', 'baz', 'foo', 'qux']), np.array(['one','one','one','one',])])

df2 = pd. DataFrame(np.random.randn(4, 4), 
                      index=[np.array(['bar', 'baz', 'foo', 'qux']), np.array(['two','two','two','two'])])

l = [df1,df2]

where df1 is
                0         1         2         3
bar one  0.027394  0.519734 -0.479718 -2.261858
baz one -0.259952 -1.790802  0.017535 -0.720666
foo one -0.756650  1.116440 -0.400762 -1.190532
qux one -0.845879  1.076155 -0.875078  1.529870

and df2
                0         1         2         3
bar two -0.443989  1.031419 -0.303421  0.360927
baz two -0.646968  0.616669  0.380372 -0.828982
foo two -0.953993 -0.664360 -0.673810  0.569510
qux two -0.522093 -0.680101  0.303099  1.118680

suppose the 0-level index and column names are the same among all the data frames, I want to combine these to get something like
                0         1         2         3
bar one  0.027394  0.519734 -0.479718 -2.261858
    two -0.443989  1.031419 -0.303421  0.360927
baz one -0.259952 -1.790802  0.017535 -0.720666
    two -0.646968  0.616669  0.380372 -0.828982
foo one -0.756650  1.116440 -0.400762 -1.190532
    two -0.953993 -0.664360 -0.673810  0.569510
qux one -0.845879  1.076155 -0.875078  1.529870
    two -0.522093 -0.680101  0.303099  1.118680

how can I do that?  

A:

You may check with concat + sort_index
df=pd.concat([df1,df2]).sort_index(level=0,axis=0)
df
Out[191]: 
                0         1         2         3
bar one -0.036748  1.505823  1.085209 -0.720105
    two  0.063424  0.202523  1.324711 -1.088452
baz one -0.023442 -0.340350  1.303056  0.725405
    two  0.608751  1.880995  0.001719 -0.133119
foo one -1.222408 -1.264111 -1.039589  0.387900
    two  0.915225  0.700590  0.996271  0.161716
qux one  0.252766  2.542727  1.119359 -0.707420
    two  0.529703  0.984748  0.086643  0.484503

