Please Steal My Code: Concatenating Dataframes in Pandas
If you concatenate two dataframes, a new dataframe will be created with the rows of the first dataframe above the rows of the second dataframe.
The code you probably want
To concatenate two dataframes while keeping only the columns they share:
import pandas as pd
df1 = pd.DataFrame({'A': [1, 2], 'B': ['a', 'b']})
df2 = pd.DataFrame({'A': [3, 4], 'B': ['c', 'd']})
concatenated_dataframe = pd.concat([df1, df2], ignore_index=True)
You can find more blogs like this here.
Other Common Use Cases
Only keep columns that are present in all dataframes
import pandas as pd
df1 = pd.DataFrame({'A': [1, 2], 'B': ['a', 'b']})
df2 = pd.DataFrame({'A': [3, 4], 'C': [True, False]})
concatenated_dataframe = pd.concat([df1, df2], ignore_index=True, join='inner')
Keep original index values
import pandas as pd
df1 = pd.DataFrame({'A': [1, 2], 'B': ['a', 'b']})
df2 = pd.DataFrame({'A': [3, 4], 'B': ['c', 'd']})
concatenated_dataframe = pd.concat([df1, df2])
Concatenate more than two dataframes
import pandas as pd
df1 = pd.DataFrame({'A': [1, 2], 'B': ['a', 'b']})
df2 = pd.DataFrame({'A': [3, 4], 'B': ['c', 'd']})
df3 = pd.DataFrame({'A': [5, 6], 'B': ['e', 'f']})
concatenated_dataframe = pd.concat([df1, df2, df3])
What is this blog post
At Mito, we’ve spent years working with users to automate their data work in Python. As such, we’ve got a great sense of what the most common use cases are for common pandas functions.
This blog post is the first in a series of tutorials — designed to be the best pandas documentation for you most of the time. There’s no BS marketing langauge at the top of this blog post, and we rank (easy to copy!) code snippets depending on how likely you are to be encountering them.
If you’re looking to save time writing Python code (beyond just saving time on reading docs), we recommend checking out our product Mito — we built it with the same set of learnings that helped us build the best pandas tutorials ever! You can see our documentation for concatenating dataframes easily in Mito here.