title: Python Pandas concatenate two multi index dataframe into one with another level of multi index
body: <p>I am dealing with Multi Index again. This time, I am concating two dataframes with same types Multi-indexed index and columns. However, the values are different, and there will be come index.level(0) values are different. I want to concate two dataframe into one. Please take a look at below example.</p>
  
  <pre><code>In [13]: df_ver1
  Out[13]: 
  key  nm         0         1         2         3
  bar one -0.424972  0.567020  0.276232 -1.087401
      two -0.673690  0.113648 -1.478427  0.524988
  baz one  0.404705  0.577046 -1.715002 -1.039268
      two -0.370647 -1.157892 -1.344312  0.844885
  foo one  1.075770 -0.109050  1.643563 -1.469388
      two  0.357021 -0.674600 -1.776904 -0.968914
  qux one -1.294524  0.413738  0.276662 -0.472035
      two -0.013960 -0.362543 -0.006154 -0.923061
  
  
  In [14]: df_ver2
  Out[14]: 
  key  nm         0         1         2         3
  bar one  0.895717  0.410835 -1.413681 -1.236269
      two  0.805244  0.813850  1.607920  0.896171
  baz one -1.206412  0.132003  1.024180 -0.487602
      two  2.565646 -0.827317  0.569605 -0.082240
  oof one  1.431256 -0.076467  0.875906 -2.182937
      two  1.340309 -1.187678 -2.211372  0.380396
  qux one -1.170299  1.130127  0.974466  0.084844
      two -0.226169 -1.436737 -2.006747  0.432390
  
  
  In [15]: df_total
  out[15]:
  key  nm  ver            0         1         2         3
  bar one ver1    -0.424972  0.567020  0.276232 -1.087401
          ver2     0.895717  0.410835 -1.413681 -1.236269
      two ver1    -0.673690  0.113648 -1.478427  0.524988
          ver2     0.805244  0.813850  1.607920  0.896171
  baz one ver1     0.404705  0.577046 -1.715002 -1.039268
          ver2    -1.206412  0.132003  1.024180 -0.487602
      two ver1    -0.370647 -1.157892 -1.344312  0.844885
          ver2     2.565646 -0.827317  0.569605 -0.082240
  qux one ver1    -1.294524  0.413738  0.276662 -0.472035
          ver2    -1.170299  1.130127  0.974466  0.084844
      two ver1    -0.013960 -0.362543 -0.006154 -0.923061
          ver2    -0.226169 -1.436737 -2.006747  0.432390
  </code></pre>
  
  <p>As you can already see, two levels of Multi-index and the third level would indicate the dataframe version as ver1 or ver2. Each values are layed in respect to columns. One thing to keep in mind is that in df_ver1, there are foo index and in df_ver2, there are oof index. These indexes does not have matching index to other dataframe, so it is concatenated in inner frames. I hope I explained well enough, and let me know if you have any questions. Thanks for the help!</p>