将多个 pandas 数据框组合成一个多索引数据框 [英] Combine multiple pandas DataFrames into a multi-index DataFrame
本文介绍了将多个 pandas 数据框组合成一个多索引数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有三个具有相同索引,索引名称和列名称的数据框:
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
我想将所有3个数据合并到一个多索引数据框中,其中旧索引现在是一列,新索引现在是['DF1','DF2','DF3'].>
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
最简单的方法是什么?
推荐答案
IIUC
l=[DF1,DF2,DF3]
pd.concat(l,keys= ['DF1', 'DF2', 'DF3'],axis=0).reset_index(level=1)
这篇关于将多个 pandas 数据框组合成一个多索引数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文