merging 2 dataframes vertically [duplicate]

Posted on

Question :

merging 2 dataframes vertically [duplicate]

I have 2 dataframes that have 2 columns each (same column names). I want to merge them vertically to end up having a new dataframe.

When doing

newdf = df.merge(df1,how='left',on=['Col1','Col2'])

The new df has only the rows from df and none of the rows from df1. Any reasons why this might happen?

Col1    Col2
asd     1232
cac     2324
.....

and the df1 is:

Col1    Col2
afaf    1213
asas    4353

The new dataframe newdf should be:

Col1   Col2
asd     1232
cac     2324
afaf    1213
asas    4353

Answer #1:

You could use append and use ignore_index if you don’t want to use the index values as is.

In [14]: df1.append(df2)
Out[14]:
   Col1  Col2
0   asd  1232
1   cac  2324
0  afaf  1213
1  asas  4353

In [15]: df1.append(df2, ignore_index=True)
Out[15]:
   Col1  Col2
0   asd  1232
1   cac  2324
2  afaf  1213
3  asas  4353

or use pd.concat

In [16]: pd.concat([df1, df2])
Out[16]:
   Col1  Col2
0   asd  1232
1   cac  2324
0  afaf  1213
1  asas  4353

In [17]: pd.concat([df1, df2], ignore_index=True)
Out[17]:
   Col1  Col2
0   asd  1232
1   cac  2324
2  afaf  1213
3  asas  4353
Answered By: Zero

Leave a Reply

Your email address will not be published.