Append Column from Other pandas DataFrame in Python (Example Code)

In this tutorial, I’ll explain how to add a column from another pandas DataFrame in the Python programming language.

Setting up the Example

import pandas as pd                         # Load pandas library
df1 = pd.DataFrame({"A":range(300, 305),    # Construct two pandas DataFrames
                    "B":range(100, 105)})
print(df1)
#      A    B
# 0  300  100
# 1  301  101
# 2  302  102
# 3  303  103
# 4  304  104
df2 = pd.DataFrame({"C":range(55, 50, - 1),
                    "D":range(65, 60, - 1)})
print(df2)
#     C   D
# 0  55  65
# 1  54  64
# 2  53  63
# 3  52  62
# 4  51  61

Example: Concatenating Column from Another pandas DataFrame

df1["C"] = df2["C"]                         # Appending column from second to first DataFrame
print(df1)
#      A    B   C
# 0  300  100  55
# 1  301  101  54
# 2  302  102  53
# 3  303  103  52
# 4  304  104  51

Further Resources & Related Tutorials

Please find some related Python programming tutorials on topics such as data conversion and naming data in the following list:

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