You may encounter errors if your column, other than the index, has cells with NaN.
print df1 Team Year foo 0 Hawks 2001 5 1 Hawks 2004 4 2 Nets 1987 3 3 Nets 1988 6 4 Nets 2001 8 5 Nets 2000 10 6 Heat 2004 6 7 Pacers 2003 12 8 Problem 2112 NaN print df2 Team Year foo 0 Pacers 2003 12 1 Heat 2004 6 2 Nets 1988 6 3 Problem 2112 NaN new = df1.merge(df2,on=['Team','Year'],how='left') print new[new.foo_y.isnull()] Team Year foo_x foo_y 0 Hawks 2001 5 NaN 1 Hawks 2004 4 NaN 2 Nets 1987 3 NaN 4 Nets 2001 8 NaN 5 Nets 2000 10 NaN 6 Problem 2112 NaN NaN
The command task in 2112 does not matter for foo in any table. Thus, the left join here will falsely return this row, which matches in both DataFrames, as not present in the correct DataFrame.
Decision:
What I am doing is adding a unique column to the internal DataFrame and setting the value for all rows. Then, when you join, you can check if this column is NaN for the internal table to find unique records in the external table.
df2['in_df2']='yes' print df2 Team Year foo in_df2 0 Pacers 2003 12 yes 1 Heat 2004 6 yes 2 Nets 1988 6 yes 3 Problem 2112 NaN yes new = df1.merge(df2,on=['Team','Year'],how='left') print new[new.in_df2.isnull()] Team Year foo_x foo_y in_df1 in_df2 0 Hawks 2001 5 NaN yes NaN 1 Hawks 2004 4 NaN yes NaN 2 Nets 1987 3 NaN yes NaN 4 Nets 2001 8 NaN yes NaN 5 Nets 2000 10 NaN yes NaN
NB. The problem string is now properly filtered because it matters to in_df2.
Problem 2112 NaN NaN yes yes
source share