Anti-Join Pandas

前端 未结 6 1082
梦谈多话
梦谈多话 2020-12-25 10:36

I have two tables and I would like to append them so that only all the data in table A is retained and data from table B is only added if its key is unique (Key values are u

6条回答
  •  再見小時候
    2020-12-25 11:09

    indicator = True in merge command will tell you which join was applied by creating new column _merge with three possible values:

    • left_only
    • right_only
    • both

    Keep right_only and left_only. That is it.

    outer_join = TableA.merge(TableB, how = 'outer', indicator = True)
    
    anti_join = outer_join[~(outer_join._merge == 'both')].drop('_merge', axis = 1)
    
    
    

    easy!

    Here is a comparison with a solution from piRSquared:

    1) When run on this example matching based on one column, piRSquared's solution is faster.

    2) But it only works for matching on one column. If you want to match on several columns - my solution works just as fine as with one column.

    So it's up for you to decide.

提交回复
热议问题