[英]Combine multiple pandas DataFrames into a multi-index DataFrame
我有三個具有相等索引,索引名稱和列名稱的數據框:
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']。
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
最簡單的方法是什么?
聯合會
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.