0

I've got two data frames that I want to compare and find the differences, but I am only really interested in which ones are from DF1 need updating to DF2.

I know that both contain the same indexes but the data inside MAY differ

DF1

+-----+-------+-----+
| Id  | Name  | Age |
+-----+-------+-----+
| 2   | Fred  | 36  |
+-----+-------+-----+
| 3   | Amy   | 16  |
+-----+-------+-----+

DF2

+-----+-------+-----+
| Id  | Name  | Age |
+-----+-------+-----+
| 2   | Fred  | 36  |
+-----+-------+-----+
| 3   | Amy   | 50  |
+-----+-------+-----+

Notice Amy's age has changed but the all the other data has stayed the same.

I can merge with an indicator to see which rows are in the left or right or both.

df1.merge(df1, how="outer", indicator=True)

But that doesn't help me in this situation.

Is there an easy way to compare both dataframes id's and check for changes.

My expected Output would be:

+-----+-------+-----+
| Id  | Name  | Age |
+-----+-------+-----+
| 3   | Amy   | 16  |
+-----+-------+-----+

Because ID = 3 from DF1 needs to be updated into DF2

Lewis Morris
  • 1,427
  • 1
  • 20
  • 28

0 Answers0