Concatenate Two pandas DataFrames with Different Columns in Python (Example Code)

In this tutorial, I’ll show how to combine two pandas DataFrames with different column names in the Python programming language.

Setting up the Example

import pandas as pd                                             # Load pandas library
my_df1 = pd.DataFrame({"A":["yes", "yes", "no", "yes", "yes"],  # Construct two pandas DataFrames
                     "B":range(10, 15),
                     "C":["hey", "hi", "huhu", "huhu", "hello"]})
print(my_df1)
#      A   B      C
# 0  yes  10    hey
# 1  yes  11     hi
# 2   no  12   huhu
# 3  yes  13   huhu
# 4  yes  14  hello
my_df2 = pd.DataFrame({"A":["foo", "bar", "foo", "foo"],
                     "X":range(11, 15)})
print(my_df2)
#      A   X
# 0  foo  11
# 1  bar  12
# 2  foo  13
# 3  foo  14

Example: Appending Two pandas DataFrames with Different Variables

my_df_combined = pd.concat([my_df1, my_df2])                    # Combine two DataFrames
print(my_df_combined)
#      A     B      C     X
# 0  yes  10.0    hey   NaN
# 1  yes  11.0     hi   NaN
# 2   no  12.0   huhu   NaN
# 3  yes  13.0   huhu   NaN
# 4  yes  14.0  hello   NaN
# 0  foo   NaN    NaN  11.0
# 1  bar   NaN    NaN  12.0
# 2  foo   NaN    NaN  13.0
# 3  foo   NaN    NaN  14.0

Related Articles & Further Resources

Here, you can find some further resources on topics such as data conversion and lists:

Leave a Reply

Your email address will not be published.

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