Dictionary of Pandas Dataframes to MultiIndex Dataframe

前端 未结 1 889
醉梦人生
醉梦人生 2021-02-19 07:07

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

1条回答
  •  别那么骄傲
    2021-02-19 07:50

    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
    

    0 讨论(0)
提交回复
热议问题