0

HJello guys,

I wanzt to do a left join like below, but im gotting date1_x, date1_y and date2_x and date2_y. What i want is to replace the column if it exists.

df1 = df1.merge(df2, on='id', how='left')

df1

id, date1, val1, date2 1, '2021-03-10', 'XEP', '2021-04-10'

df2

 id,    date1,      date2
 1,  '2021-09-03', '2021-09-04'
 2,  '2021-09-05', '2021-09-06'
 3,  '2021-09-07', '2021-09-08'

Resulting df:

1,  '2021-09-03', 'XEP' , '2021-09-04'
tamo007
  • 276
  • 1
  • 12
  • Does this answer your question? [Pandas Merge - How to avoid duplicating columns](https://stackoverflow.com/questions/19125091/pandas-merge-how-to-avoid-duplicating-columns) – Paul Dec 21 '21 at 11:00
  • Sample data and expected output is necessary for avoid not helpful answers. – jezrael Dec 21 '21 at 11:01

1 Answers1

0

I'm not sure if this is what you mean, but merging as followed should work if date1 and date2 are equal in both dataframes.

df1 = df1.merge(df2, on=['id', date1, date2], how='left')
Kylian
  • 319
  • 2
  • 14
  • no . i want to do left join . df1 and df2 both contains columns date1 and date2. I want to do leeft join with replacing values of date1 and date2 of my df1 dataframe. Thank you in advance. @kylian – tamo007 Dec 21 '21 at 11:03
  • Can't you just simply delete the date1 and date2 column in df1 and join the date1 and date2 columns from df2 then? – Kylian Dec 21 '21 at 12:04