将多数据框合并为一个多索引数据框

时间:2019-12-11 15:58:41

标签: python pandas dataframe

我有几个来自不同网站的数据框,例如:

for website1:
ws1 = pd.DataFrame.from_dict({"title":{"term1":2, "term2":1,"term3":2} , "content":{ "term2":4,"term3":12}}) 

index    title    content
______________________________
term1      2         NaN
term2      1         4
term3      2         12


for website2:
ws2 = pd.DataFrame.from_dict({"title":{"term2":1,"term4":2} , "content":{"term2":12, "term3":1,"term4":6}}) 

index    title    content
______________________________
term2      1         12
term3      NaN       1
term4      2         6

我想合并这些数据框,以便输出是多索引数据框。我的预期输出:

            website1             website2
index    title    content     title    content
_________________________________________________
term1      2         NaN        NaN       NaN
term2      1         4          1         12
term3      2         12         NaN       1
term4      NaN       NaN        2         6

1 个答案:

答案 0 :(得分:3)

用途:参考字典和pd.concat中的keys参数

d={'website1':ws1,'website2':ws2}
pd.concat(d.values(),axis=1,sort=False,keys=d.keys())

         website1         website2        
         title content    title content
term1      2.0     NaN      NaN     NaN
term2      1.0     4.0      1.0    12.0
term3      2.0    12.0      NaN     1.0
term4      NaN     NaN      2.0     6.0
​

来自文档:

  

键:序列,默认无   如果通过了多个级别,则应包含元组。使用传递的键作为最外层级别来构造层次结构索引。