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 |
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 |
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 |
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 |
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:
- Convert Data Type of pandas DataFrame Column in Python
- Drop Rows with NaN in pandas DataFrame Column in Python
- Order Rows of pandas DataFrame by Column in Python
- Calculate Column & Row Sums of pandas DataFrame in Python
- Test whether Column Name Exists in pandas DataFrame in Python
- Add Indices of pandas DataFrame as New Column in Python