9

Say I have a list of dataframes [df1, df2, df3], where each single dataframe looks as follows:

> df1 

            median   std
control        0.4   0.2
experiment     0.2   0.3

How can I create a multi-index dataframe that stitches them together? Like this:

                         df1                 df2                  df3
          control experiment  control experiment  control  experiment
median        0.4        0.2      ...       ...      ...          ...
std           0.2        0.3      ...       ...      ...          ...
2
  • Your column and index labels are also switched. Is this also the question? Commented Mar 20, 2014 at 22:08
  • Thanks @joris. I am all set. I can .T the resulting multi-index dataframe. Commented Mar 20, 2014 at 22:10

1 Answer 1

19

So you can provide the dataframes as a dict (as in duplicate question: python/pandas: how to combine two dataframes into one with hierarchical column index?), and then the dict keys are used:

pd.concat({'df1':df1, 'df2':df2, 'df3':df3}, axis=1)

or another option is to use the keys keyword argument:

pd.concat([df1, df2, df3], axis=1, keys=['df1', 'df2', 'df3'])
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.