Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Concate 2 dataframes horizontally (row and col wise)?

I have 2 dataframes

df1

  Cols/Rows   A    B    C
0         A   50  150  200
1         B  200  250  300
2         C  350  400  450

df2

  Cols/Rows    A    B    C
0         A   50  150  200
1         B  200  300  300
2         C  370  400  450

My expected output

  Cols/Rows    A    A2    B     B2    C    C2
0         A   50    50   150    150  200   200
1         B  200    200  250    300  300   300
2         C  350    370  400    400  450   450

I want to create new dataframe merging both with col and row wise. I tried to use merge() but it didn't worked

print(df2.merge(df1, how='left'))
like image 717
Tarun K Avatar asked Dec 11 '22 06:12

Tarun K


1 Answers

merge have suffixes

df1.merge(df2,on='Cols/Rows',suffixes =['','2'],how='left')
Out[225]: 
  Cols/Rows    A    B    C   A2   B2   C2
0         A   50  150  200   50  150  200
1         B  200  250  300  200  300  300
2         C  350  400  450  370  400  450
like image 118
BENY Avatar answered Mar 04 '23 07:03

BENY