Concatenate Three pandas DataFrames in Python – Append & Combine (Example Code)

This post explains how to concatenate three or more pandas DataFrames in the Python programming language.

Preparing the Example

import pandas as pd                                       # Import pandas library
my_df1 = pd.DataFrame({"A":["foo", "bar", "bar", "foo"],  # Construct three pandas DataFrames
                     "B":range(1, 5),
                     "C":["a", "b", "c", "d"]})
print(my_df1)
#      A  B  C
# 0  foo  1  a
# 1  bar  2  b
# 2  bar  3  c
# 3  foo  4  d
my_df2 = pd.DataFrame({"A":["yes", "no", "no"],
                     "B":range(1, 4),
                     "C":["hey", "hi", "huhu"]})
print(my_df2)
#      A  B     C
# 0  yes  1   hey
# 1   no  2    hi
# 2   no  3  huhu
my_df3 = pd.DataFrame({"A":["x", "y", "x"],
                     "B":range(10, 7, - 1),
                     "C":["y", "x", "y"]})
print(my_df3)
#    A   B  C
# 0  x  10  y
# 1  y   9  x
# 2  x   8  y

Example: Appending Multiple pandas DataFrames

my_df_combined = pd.concat([my_df1, my_df2, my_df3])
print(my_df_combined)                                     # Display concatenated DataFrames
#      A   B     C
# 0  foo   1     a
# 1  bar   2     b
# 2  bar   3     c
# 3  foo   4     d
# 0  yes   1   hey
# 1   no   2    hi
# 2   no   3  huhu
# 0    x  10     y
# 1    y   9     x
# 2    x   8     y

Further Resources

In addition, you may want to read the related articles on my website. You can find a selection of tutorials below:

Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top