pandas get rows which are NOT in other dataframe

后端 未结 13 1216
春和景丽
春和景丽 2020-11-22 02:17

I\'ve two pandas data frames which have some rows in common.

Suppose dataframe2 is a subset of dataframe1.

How can I get the rows of dataframe1 which

13条回答
  •  执笔经年
    2020-11-22 02:52

    How about this:

    df1 = pandas.DataFrame(data = {'col1' : [1, 2, 3, 4, 5], 
                                   'col2' : [10, 11, 12, 13, 14]}) 
    df2 = pandas.DataFrame(data = {'col1' : [1, 2, 3], 
                                   'col2' : [10, 11, 12]})
    records_df2 = set([tuple(row) for row in df2.values])
    in_df2_mask = np.array([tuple(row) in records_df2 for row in df1.values])
    result = df1[~in_df2_mask]
    

提交回复
热议问题