0

I have two dataframes, df1 and df2. One is the initial dataframe (data obtained from a source), the other is smaller and contains some mathematical transformation. For simplicity, they both have this layout:

df1:

A   B        C 
1   apple    a
2   pear     b
3   banana   c
4   berry    d
5   coconut  e
6   mango    f

df2:
A   B        C     D      E
1   apple    a     AT     14
2   pear     b     BT     DA
5   coconut  e     OT     OT
6   mango    f     MA     AP

Essentially, I would need another dataframe, i.e. df_excluded_values, that contains the rows that have been excluded from df2, based on the A column.

Thanks!

1

1 Answer 1

1

You can use ~ and isin():

df_excluded = df1[~df1['A'].isin(df2['A'].values)]

Returns the expected output for the rows, whose column 'A' values in df1 are not present in df2's column 'A':

   A       B  C
2  3  banana  c
3  4   berry  d
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.