pandas 数据框到多索引数据框的字典 [英] Dictionary of Pandas Dataframes to MultiIndex Dataframe
本文介绍了 pandas 数据框到多索引数据框的字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个Pandas 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]}
,我想像这样将其更改为MultiIndex DataFrame:
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
推荐答案
使用 pd.concat
,并将keys
参数设置为字典键:
Use pd.concat
on the dictionary values, with the keys
parameter set to the dictionary keys:
df = pd.concat(d.values(), keys=d.keys())
结果输出:
0 1 2
A 0 0 1 2
1 2 2 4
B 0 1 1 1
1 2 2 2
这篇关于 pandas 数据框到多索引数据框的字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文