Compare Multiple Columns to Get Rows that are Different in Two Pandas Dataframes

后端 未结 4 790
悲哀的现实
悲哀的现实 2021-01-14 16:38

I have two dataframes:

df1=
    A    B   C
0   A0   B0  C0
1   A1   B1  C1
2   A2   B2  C2

df2=
    A    B   C
0   A2   B2  C10
1   A1   B3  C11
2   A9   B4         


        
4条回答
  •  甜味超标
    2021-01-14 17:34

    If your version is 0.17.0 then you can use pd.merge and pass the cols of interest, how='left' and set indicator=True to whether the values are only present in left or both. You can then test whether the appended _merge col is equal to 'both':

    In [102]:
    pd.merge(df1, df2, on='A',how='left', indicator=True)['_merge'] == 'both'
    
    Out[102]:
    0    False
    1     True
    2     True
    Name: _merge, dtype: bool
    
    In [103]:
    pd.merge(df1, df2, on=['A', 'B'],how='left', indicator=True)['_merge'] == 'both'
    
    Out[103]:
    0    False
    1    False
    2     True
    Name: _merge, dtype: bool
    

    output from the merge:

    In [104]:
    pd.merge(df1, df2, on='A',how='left', indicator=True)
    
    Out[104]:
        A B_x C_x  B_y  C_y     _merge
    0  A0  B0  C0  NaN  NaN  left_only
    1  A1  B1  C1   B3  C11       both
    2  A2  B2  C2   B2  C10       both
    
    In [105]:    
    pd.merge(df1, df2, on=['A', 'B'],how='left', indicator=True)
    
    Out[105]:
        A   B C_x  C_y     _merge
    0  A0  B0  C0  NaN  left_only
    1  A1  B1  C1  NaN  left_only
    2  A2  B2  C2  C10       both
    

提交回复
热议问题