0

I have one dataframe data1

And another dataframe data2

I was trying to insert columns starting dates and ending dates from data2 into data1 only on those column(index) which matches with column(index) of data2. And keeping other unmatched column(index) as Nan. I tried merge() but it didnt work.

Sorry for unambiguous typing style. I am a newbie.

Lamanus
  • 12,898
  • 4
  • 21
  • 47
Prater
  • 107
  • 1
  • 8
  • 1
    you are exactly asking how to do the left join. – Lamanus Aug 02 '20 at 07:35
  • Hi.. Welcome to SO, don't include images. Please [`see here`](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) to learn how to write effective pandas questions. – sushanth Aug 02 '20 at 07:41

1 Answers1

0

Try:

pd.merge(df1, df2, left_on=['index'], right_on=['Unnamed: 0'], how='left')

explanation:

left_on, right_on - tell merge which exact columns to perform the matching on.

how='left' - dont drop unmatched rows(in df1) - leave new columns as None.

han solo
  • 6,390
  • 1
  • 15
  • 19
yoav_aaa
  • 367
  • 2
  • 11