I have two data frames df1 and df2 and I would like to merge them into a single data frame. It is as if df1 and df2 were created by splitting a single data frame down the center vertically, like tearing a piece of paper that contains a list in half so that half the columns go on one paper and half the columns go on the other. I would like to merge them back together. How do I do it?
Pandas has a built-in merge function. Please refer to the documentation
End-result should be something like this: df_merged = pd.merge(df1, df2, left_on=['name_indexcolumn_df1_here'], right_on=['name_indexcolumn_df2_here'], how='inner')
If you split the DataFrame "vertically" then you have two DataFrames that with the same index.
You can use the merge function or the concat function.
With concat with would be something like this:
With merge with would be something like this:
For more complex merging options see the Merge, join and concat pandas tutorial.
Considering the dataframes:
Preco2018 with size (8784, 5)
Preco 2019 with size (8760, 5)
That have the same column names.
You can combine them using
pandas.concat, by simply
import pandas as pd frames = [Preco2018, Preco2019] df_merged = pd.concat(frames)
Which results in a DataFrame with the following size (17544, 5)
If you want to visualize, it ends up working like this