Categories
Python Answers

How to merge multiple data frames with Python Pandas?

Spread the love

Sometimes, we want to merge multiple data frames with Python Pandas.

In this article, we’ll look at how to merge multiple data frames with Python Pandas.

How to merge multiple data frames with Python Pandas?

To merge multiple data frames with Python Pandas, we can use the concat method.

For instance, we write:

import pandas as pd

data1 = {'a': [1, 2, 3]}
data2 = {'a': [4, 5, 6]}
df1 = pd.DataFrame(data1)
df2 = pd.DataFrame(data2)

merged_df = pd.concat([df1, df2])
print(merged_df)

We create 2 data frames with pd.DataFrame and 2 dictionaries.

Then we call pd.concat with the data frames in the list and assign the returned data frame to merged_df.

Therefore, merged_df is:

   a
0  1
1  2
2  3
0  4
1  5
2  6

Conclusion

To merge multiple data frames with Python Pandas, we can use the concat method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *