29
votes

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
1
you want pd.concat([df1,df2]), this is a very common question, see the docs, related: stackoverflow.com/questions/11637384/… and stackoverflow.com/questions/15819050/… - EdChum
I forgot to mention that concat returns:TypeError: first argument must be an iterable of pandas objects, you passed an object of type "DataFrame" - Andrei Cozma
when you pass how='left' this only merge's horizontally on the values in those columns on the lhs, it's unclear what you really want. You can try passing 'outer' - EdChum

1 Answers

44
votes

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