pandas 如何只对符合条件的行进行合并优化?

uinbv5nw  于 2023-02-20  发布在  其他
关注(0)|答案(2)|浏览(119)

我想在列a上左合并df_1和df_2
我可以轻松地将其存档:
df_3 = df_1.merge(df_2, on="a", how="left")
但是,我知道我永远不会在df_2中找到a,当df_1.b == 0
为了优化我的代码,我希望仅在df_1.b != 0
知道了这些信息,我怎样才能更有效地得到df_3?

输入

d = {'a': list('ABCDEF'),                                                                           
     'b': list('111000')}                                                                           
df_1 = pd.DataFrame(data=d)                                                                         
                                                                                                    
#    a  b                                                                                           
# 0  A  1                                                                                           
# 1  B  1                                                                                           
# 2  C  1                                                                                           
# 3  D  0                                                                                           
# 4  E  0                                                                                           
# 5  F  0                                                                                           
                                                                                                    
d = {'a': list('ABC'),                                                                              
     'c': list('xyz')}                                                                              
df_2 = pd.DataFrame(data=d)                                                                         
                                                                                                    
#    a  c                                                                                           
# 0  A  x                                                                                           
# 1  B  y                                                                                           
# 2  C  z

预期输出

df_3                                                                                                
                                                                                                    
#    a  b    c                                                                                      
# 0  A  1    x                                                                                      
# 1  B  1    y                                                                                      
# 2  C  1    z                                                                                      
# 3  D  0  NaN                                                                                      
# 4  E  0  NaN                                                                                      
# 5  F  0  NaN
kwvwclae

kwvwclae1#

就合并它

df_1.merge(df_2, how = 'outer', on = 'a')
8yparm6h

8yparm6h2#

IIUC用途:

m = df_1.b != '0'
df_3 = (df_1[m].reset_index().merge(df_2, on="a", how="left",
                                   suffixes=('','_'))
               .drop(df_1.columns, axis=1)
               .set_index('index'))

print (df_3)
       c
index   
0      x
1      y
2      z

out = pd.concat([df_1, df_3], axis=1)
print (out)
   a  b    c
0  A  1    x
1  B  1    y
2  C  1    z
3  D  0  NaN
4  E  0  NaN
5  F  0  NaN

相关问题