简体   繁体   English

如何在两个 DataFrame 上执行完整的外部联接并仍然保留元素的相对顺序?

[英]How can I execute a full outer join on two DataFrames and still preserve the relative order of the elements?

Let's consider two dataframe d1 and d2 , I want to merge them into a single dataframe让我们考虑两个dataframe d1d2 ,我想将它们合并为一个dataframe

d1 : d1

Id      Country       P_Type      
102     Portugal      Industries  
163     Portugal      Office 
111     Portugal      Clubs       
164     Portugal      cars 
168     Finland       Houses

d2 : d2

Id      Country       P_Type      Sales
102     Portugal      Industries  1651
163     Portugal      Office      1125
111     Portugal      Clubs       1752
164     Portugal      cars        1259
129     Sweden        Pubs        1345 
105     Germany       Industries  1451
103     Germany       Office      1635
103     Germany       Clubs       1520
103     Germany       cars        1265

I tired the pd.merge :我厌倦了pd.merge

result=pd.merge(d2,d1,how='outer',on=["Id"])

I got the results:我得到了结果:

Id      Country       P_Type      Sales
102     Portugal      Industries  1651
163     Portugal      Office      1125
111     Portugal      Clubs       1752
164     Portugal      cars        1259
105     Germany       Industries  1451
103     Germany       Office      1635
129     Sweden        Pubs        1345
103     Germany       Clubs       1520
103     Germany       cars        1265
168     Finland       Houses      Nan

what I expected is:我的预期是:

Id      Country       P_Type      Sales
102     Portugal      Industries  1651
163     Portugal      Office      1125
111     Portugal      Clubs       1752
164     Portugal      cars        1259
168     Finland       Houses      Nan
105     Germany       Industries  1451
129     Sweden        Pubs        1345
103     Germany       Office      1635
103     Germany       Clubs       1520
103     Germany       cars        1265

Even if I try to cross it the other way around ie d1 x d2 the relative positioning of id number 129 would get altered.即使我尝试以相反的方式越过它,即d1 x d2 ,id 编号 129 的相对位置也会改变。

Just swap d2 and d1 :只需交换d2d1

result = d1.merge(d2, how='outer')
print(result)

# Output
    Id   Country      P_Type   Sales
0  102  Portugal  Industries  1651.0
1  163  Portugal      Office  1125.0
2  111  Portugal       Clubs  1752.0
3  164  Portugal        cars  1259.0
4  168   Finland      Houses     NaN
5  105   Germany  Industries  1451.0
6  103   Germany      Office  1635.0
7  103   Germany       Clubs  1520.0
8  103   Germany        cars  1265.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM