Eliminate partial match pandas data rows

I want to compare two pandas frames and find out the lines that are only in df1 by comparing the values ​​in columns A and B. I feel that I can somehow accomplish this using merge, but I can not understand.

import pandas as pd

df1 = pd.DataFrame([[1,11, 111], [2,22, 222], [3, 33, 333]], columns=['A', 'B', 'C'])
df2 = pd.DataFrame([[1, 11]], columns=['A', 'B'])

df1

    A   B   C
0   1   11  111
1   2   22  222
2   3   33  333

df2

    A   B
0   1   11

Dataframe I want to see

    A   B   C
1   2   22  222
2   3   33  333
+4
source share
3 answers

Based on this approach :

import pandas as pd

df1 = pd.DataFrame([[1,11, 111], [2,22, 222], [3, 33, 333]], columns=['A', 'B', 'C'])
df2 = pd.DataFrame([[1, 11]], columns=['A', 'B'])

Combine data:

df = pd.concat([df1, df2])
df.reset_index(drop=True)

Group the required comparison columns - in your case, Aand B:

df_gpby = df.groupby(['A','B'])

Get codes for groups with one meaning - that is unique in pairs A, B:

idx = [x[0] for x in df_gpby.groups.values() if len(x) == 1]

Adjust concatenated index data frame:

df.iloc[idx]

Results in:

    A   B   C
1   2   22  222
2   3   33  333
+1

vmg , , , . :

:

In [46]: df3 = df1.subtract(df2)

In [47]: df3
Out[47]: 
    A   B   C
0   0   0 NaN
1 NaN NaN NaN
2 NaN NaN NaN

, - , df2, NaN. numpy, :

In [50]: np.isnan(df3.iloc[0])
Out[50]: 
A    False
B    False
C     True
Name: 0, dtype: bool

In [51]: np.isnan(df3.iloc[1])
Out[51]: 
A    True
B    True
C    True
Name: 1, dtype: bool

, , , :

In [52]: df1.iloc[[idx for idx, row in df3.iterrows() if 
                  all(np.isnan(df3.iloc[idx]))]]
Out[52]: 
   A   B    C
1  2  22  222
2  3  33  333

update,

def substract_dataframe(df1, df2):
    for i in [df1, df2]:
        if not isinstance(i, pd.DataFrame):
            raise ValueError(("Wrong argument given! 
                             All arguments must be DataFrame instances"))
    df = df1.subtract(df2)
    return df1.iloc[[idx for idx, row in df.iterrows() if      
                    all(np.isnan(df.iloc[idx]))]]

...

In [54]: substract_dataframe(df1, df2)
Out[54]: 
   A   B    C
1  2  22  222
2  3  33  333

In [55]: substract_dataframe(df1, 'sdf')
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-55-6ce801e88ce4> in <module>()
----> 1 substract_dataframe(df1, 'sdf')

<ipython-input-53-e5d7db966311> in substract_dataframe(df1, df2)
      2     for i in [df1, df2]:
      3         if not isinstance(i, pd.DataFrame):
----> 4             raise ValueError("Wrong argument given! All arguments must be DataFrame instances")
      5     df = df1.subtract(df2)
      6     return df1.iloc[[idx for idx, row in df.iterrows() if all(np.isnan(df.iloc[idx]))]]

ValueError: Wrong argument given! All arguments must be DataFrame instances
+1

merge indicator, 0.17.0 version

In [3544]: df1.merge(df2, how='left', indicator='_b').query('_b != "both"').drop('_b', 1)
Out[3544]:
   A   B    C
1  2  22  222
2  3  33  333

In [3545]: df1.merge(df2, how='left', indicator='_b')
Out[3545]:
   A   B    C         _b
0  1  11  111       both
1  2  22  222  left_only
2  3  33  333  left_only

In [3546]: df1.merge(df2, how='left', indicator='_b').query('_b != "both"')
Out[3546]:
   A   B    C         _b
1  2  22  222  left_only
2  3  33  333  left_only
+1

Source: https://habr.com/ru/post/1612651/


All Articles