Skip to content
Advertisement

How to join two dataframe on different columns without using index

i have following 2 dataframes and i want to merge them.

df1 = pd.DataFrame({'A': ['A0', 'A1', 'A2', 'A3'],
'B': ['B0', 'B1', 'B2', 'B3'],
'C': ['C0', 'C1', 'C2', 'C3'],
'D': ['D0', 'D1', 'D2', 'D3']},
index=[0, 1, 2, 3])
df2 = pd.DataFrame({'z': ['A4', 'A5', 'A6', 'A7'],
'e': ['B4', 'B5', 'B6', 'B7'],
'y': ['C4', 'C5', 'C6', 'C7'],
   'f': ['D4', 'D5', 'D6', 'D7']},
index=[12,2, 43,24])

    A   B   C   D
0  A0  B0  C0  D0
1  A1  B1  C1  D1
2  A2  B2  C2  D2
3  A3  B3  C3  D3
    z   y   f
12  B4  C4  D4
32  B5  C5  D5
43  B6  C6  D6
24  B7  C7  D7

And i want like :

      A   B   C   D   z   y   f
   0  A0  B0  C0  D0  B4  C4  D4
   1  A1  B1  C1  D1  B5  C5  D5
   2  A2  B2  C2  D2  B6  C6  D6
   3  A3  B3  C3  D3  B7  C7  D7

can anyone help me , tried below code but i didnt get the solution

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

Blockquote

Advertisement

Answer

You need reset_index first

df=pd.concat([df1.reset_index(drop=True),df2.reset_index(drop=True)],axis=1)
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement