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
To concatenate DataFrames vertically in Pandas, use the concat(~) method.
To concatenate DataFrames, use the concat() method, but to ignore duplicates, use the drop_duplicates() method.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With