pandas get rows which are NOT in other dataframe

后端 未结 13 881
春和景丽
春和景丽 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 03:01

    you can do it using isin(dict) method:

    In [74]: df1[~df1.isin(df2.to_dict('l')).all(1)]
    Out[74]:
       col1  col2
    3     4    13
    4     5    14
    

    Explanation:

    In [75]: df2.to_dict('l')
    Out[75]: {'col1': [1, 2, 3], 'col2': [10, 11, 12]}
    
    In [76]: df1.isin(df2.to_dict('l'))
    Out[76]:
        col1   col2
    0   True   True
    1   True   True
    2   True   True
    3  False  False
    4  False  False
    
    In [77]: df1.isin(df2.to_dict('l')).all(1)
    Out[77]:
    0     True
    1     True
    2     True
    3    False
    4    False
    dtype: bool
    

提交回复
热议问题