简体   繁体   中英

Dictionary of Pandas Dataframes to MultiIndex Dataframe

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM