Ready to write Python code 4x faster?
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)
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])
See more common dataframe concatenation examples here.
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 reccomend 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 documentatio for concatenating dataframes easily in Mito here.
More Like This
Automating Spreadsheets with Python 101
How to tell the difference between a good and bad Python automation target.
10 Mistakes To Look Out For When Transitioning from Excel To Python
10 Common Mistakes for new programmers transitioning from Excel to Python
Research shows Mito speeds up by 400%
We're always on the hunt for tools that improve our efficiency at work. Tools that let us accomplish more with less time, money, and resources.
3 Rules for Choosing Between SQL and Python
Analysts at the world's top banks are automating their manual Excel work so they can spend less time creating baseline reports, and more time building new analyses that push the company forward.