0

Still learning pandas, so please excuse if the question seems naive. I have two dataframes with different number of rows. I want to join them along axis 1.

df1:

Myidx col1 col2 col3
0    a     b    c
1    d     e    f
2    g     h    i
3    j     k    l

df2:

idx col4  col5  col6
 0     m      n     p

resulting df should look like:

idx col1 col2 col3  col4  col5  col6
0    a     b    c    m     n     p
1    d     e    f
2    g     h    i
3    j     k    l

I am able to append it to the bottom but joining them along axis1 is not working. I tried using following two things. (The resulting csv file does not even show df2)

1) df1.join(df2)
2) pd.concat([df1,df2], axis =1)

Any help is highly appreciated.

Thanks,

Nick ODell
  • 15,465
  • 3
  • 32
  • 66
Aisha
  • 91
  • 7

1 Answers1

0
dfnew = df1.merge(df2, left_on='Myidx', right_on='idx', suffixes=(False, False)).reset_index()

should do you

or look at the merging guide.

Pandas Merging 101

Paul Brennan
  • 2,638
  • 4
  • 19
  • 26