[英]Combine multiple pandas DataFrames into a multi-index DataFrame
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
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']. 我想将所有3个都合并到一个多索引数据框中,其中旧索引现在是一列,新索引现在是['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
What's the easiest way to go about this? 最简单的方法是什么?
IIUC 联合会
l=[DF1,DF2,DF3]
pd.concat(l,keys= ['DF1', 'DF2', 'DF3'],axis=0).reset_index(level=1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.