0

I want to merge two dataframes, but under certain conditions:

If the values of the columns from dataframe 2 (Source1,Target1,Source2,Target2) occur in dataframe 1, then I want to replace them with the data from dataframe 2, but merge them with all columns from dataframe 2 and all columns from dataframe 1. My current problem is when I do a concatenation, the data from DF2 is only merged with that from DF1 and I have invalid data.

In short: match DF1 with DF2 and if there are intersections, then overwrite the intersection from in DF1, but merge all columns from DF2 with those from DF1.

Thanks for your help

DF1

DF1

DF2

DF2

What I get

What I get

What I need

What I need

frames = [DF1,DF2]
result = pd.concat(frames)
print(result)
Corralien
  • 109,409
  • 8
  • 28
  • 52
densing
  • 15
  • 3

1 Answers1

0

Use merge:

out = pd.merge(DF1, DF2, how='left',
               on=['Source 1', 'Target 1', 'Source 2', 'Target 2'])

Take a while to read Pandas Merging 101

Corralien
  • 109,409
  • 8
  • 28
  • 52