-1

Imagine I have two pandas dataframes with these values:

Df1

id  value

1    45
2    48
3    78

Df2

id  value

1    45
2    49
3    78

We can see that there are two similar values (for id 1 and 3), and one different.

I want the Df1 to become like this:

id  value

1    45
2    48
2    49
3    78

Any ideas how I can do that?

Thanks a lot !!

Abilys38
  • 405
  • 8
  • 20
  • You can see that the condition is the value. If the value is different, then we add for the same id a second value. I would say it's a merge but with the condition of a different value – Abilys38 Dec 17 '18 at 11:32

2 Answers2

2

You need merge

print(df1.merge(df2, how='outer', on=['value','id']))

Output:

    value   id
0   45      1
1   48      2
2   78      3
3   49      2
Sociopath
  • 12,395
  • 17
  • 43
  • 69
1

You can concatenate, sort by id (if necessary), then drop_duplicates:

df = pd.concat([df1, df2]).sort_values('id').drop_duplicates()

print(df)

   id  value
0   1     45
1   2     48
1   2     49
2   3     78
jpp
  • 147,904
  • 31
  • 244
  • 302