23

I have a dict of Pandas Dataframes, say

d = {A: pd.DataFrame([[0, 1, 2], [2, 2, 4]),
     B: pd.DataFrame([[1, 1, 1], [2, 2, 2]}

and I'd like to change it into a MultiIndex DataFrame like this:

A 0   0, 1, 2
  1   2, 2, 4
B 0   1, 1, 1
  1   2, 2, 2

1 Answer 1

32

Use pd.concat on the dictionary values, with the keys parameter set to the dictionary keys:

df = pd.concat(d.values(), keys=d.keys())

The resulting output:

     0  1  2
A 0  0  1  2
  1  2  2  4
B 0  1  1  1
  1  2  2  2
Sign up to request clarification or add additional context in comments.

1 Comment

pd.concat(d) should produce the same result set

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.