How to implement left outer join in python pandas? [duplicate] How to implement left outer join in python pandas? [duplicate] pandas pandas

How to implement left outer join in python pandas? [duplicate]


set param indicator=True, this will add a column _merge you then filter just the rows that are left_only:

In [46]:df1 = pd.DataFrame({'key': ['A', 'B', 'C', 'D'],'value1': np.random.randn(4)})​df2 = pd.DataFrame({'key': ['B', 'D', 'D', 'E'], 'value2': np.random.randn(4)})​df3 = df1.merge(df2, on=['key'], how='left', indicator=True)df3Out[46]:  key    value1    value2     _merge0   A -0.346861       NaN  left_only1   B  1.120739  0.558272       both2   C  0.023881       NaN  left_only3   D -0.598771 -0.823035       both4   D -0.598771  0.369423       bothIn [48]:df3[df3['_merge'] == 'left_only']Out[48]:  key    value1  value2     _merge0   A -0.346861     NaN  left_only2   C  0.023881     NaN  left_only

if on older version then use isin with ~ to negate the mask:

In [50]:df3[~df3['key'].isin(df2['key'])]Out[50]:  key    value1  value20   A -0.346861     NaN2   C  0.023881     NaN