DataFrame合并在不相等的键上

时间:2017-04-04 16:16:11

标签: python pandas merge

我得到了表A:

 A = pd.DataFrame({'a':[1,2,3,4,5],
                       'b':['II','I','I','III','II'],
                       'c':['I', 'II', 'II', 'III', 'II'],
                       'd':[0,2,0,2,0],
                       'e':[0,2,0,3,0],
                       'f':[0,3,0,4,0],})

简而言之,我想在pandas中编写这个SQL代码:

proc sql;
    create table MERGED as
    select distinct T1.*,
    T2.a as a_merged
    from A as T1
    left join A as T2
    on T1.b = T2.b and T1.c = T2.c and 
    T2.d = 0 and T2.e = 0 and T1.f ^= T2.f;
run;

问题是:我不知道(我找不到),如何用pd.merge实现3,4和最后条件。 前两个很简单......:

MERGED = A.merge(A, how = 'left', on = ['b', 'c'] ,suffixes=('', '_r'))

所以结果应该是:

B = pd.DataFrame({'a':[1,2,3,4,5],
                       'b':['II','I','I','III','II'],
                       'c':['II', 'II', 'II', 'III', 'II'],
                       'd':[1,2,0,2,0],
                       'e':[0,2,0,3,0],
                       'f':[0,3,0,4,0],
                 'a_r':['',2,'','','']})

0 个答案:

没有答案