Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 
like image 754
Andrei Cozma Avatar asked Dec 16 '16 10:12

Andrei Cozma


People also ask

How do I merge two DataFrames vertically?

To concatenate DataFrames vertically in Pandas, use the concat(~) method.

How do I merge two DataFrames without duplicates?

To concatenate DataFrames, use the concat() method, but to ignore duplicates, use the drop_duplicates() method.

How do I merge DataFrames without duplicating columns?

merge() function to join the left dataframe with the unique column dataframe using 'inner' join. This will ensure that no columns are duplicated in the merged dataset.


1 Answers

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 
like image 97
Zero Avatar answered Oct 05 '22 02:10

Zero