5

I have three dataframes with equivalent indices, index names and column names:

DF1:
         value
index
    0       a0
    1       a1
    2       a2
    3       a3

DF2:
         value
index
    0       b0
    1       b1
    2       b2
    3       b3

DF3:
         value
index
    0       c0
    1       c1
    2       c2
    3       c3

I'd like to combine all 3 into a single multi-index dataframe, where the old index is now a column, and the new index is now ['DF1', 'DF2', 'DF3'].

             old_index     value
new_index
      DF1            0        a0
                     1        a1
                     2        a2
                     3        a3
      DF2            0        b0
                     1        b1
                     2        b2
                     3        b3
      DF3            0        c0
                     1        c1
                     2        c2
                     3        c3

What's the easiest way to go about this?

1 Answer 1

19

IIUC

l=[DF1,DF2,DF3]

pd.concat(l,keys= ['DF1', 'DF2', 'DF3'],axis=0).reset_index(level=1)
Sign up to request clarification or add additional context in comments.

2 Comments

Wow, that was fast!
@cᴏʟᴅsᴘᴇᴇᴅ Thank you ~ :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.