0

Suppose I have two dataframes

A:

column1 column2 
  abc      2
  def      2

B:

column1 column2 
  abc      2
  def      1

I want to compare these two dataframes and find where there are differences and get the value of column1.

So the output should be 'def' in this case

Grayrigel
  • 3,474
  • 5
  • 14
  • 32
Graleon3
  • 47
  • 5

2 Answers2

0

Based on this answer here, you can try pd.concat method:

pd.concat([A,B]).drop_duplicates(keep=False)['column1'].unique().tolist()

Output:

# if you just want to see the differences between the dataframe
>>> pd.concat([A,B]).drop_duplicates(keep=False)
  column1  column2
1     def        2
1     def        1
# if you just want to see the differences and with only 'column1'
>>> pd.concat([A,B]).drop_duplicates(keep=False)['column1']
1    def
1    def
Name: column1, dtype: object
# if you want unique values in the column1 as a numpy array after taking the differences
>>> pd.concat([A,B]).drop_duplicates(keep=False)['column1'].unique()
array(['def'], dtype=object) 
# if you want unique values in the column1 as a list after taking the differences
>>> pd.concat([A,B]).drop_duplicates(keep=False)['column1'].unique().tolist() 
['def']
Grayrigel
  • 3,474
  • 5
  • 14
  • 32
  • Added an answer. Let me know if it works for you. It will give a list as an output. If it does please accept/check-mark the answer. – Grayrigel Nov 05 '20 at 11:00
0
pd.concat([A,B]).drop_duplicates(keep=False)
MUK
  • 371
  • 4
  • 13