How to add columns from one DataFrame to another?

Adding columns from one DataFrame to another can be done by using the pandas “concat” function. This function takes two DataFrames as arguments and concatenates them together by adding the columns from the second DataFrame to the first. The index of the resulting DataFrame will be the union of the two original DataFrames. The original column order is preserved in the combined DataFrame. It is important to note that the column names must be unique in the combined DataFrame or else a ValueError will be raised.


You can use one of the following two methods to add a column from one pandas DataFrame to another DataFrame:

Method 1: Add Column from One DataFrame to Last Column Position in Another

#add some_col from df2 to last column position in df1
df1['some_col']= df2['some_col']

Method 2: Add Column from One DataFrame to Specific Position in Another

#insert some_col from df2 into third column position in df1
df1.insert(2, 'some_col', df2['some_col'])

The following examples show how to use each method in practice with the following pandas DataFrames:

import pandas as pd

#create first DataFrame
df1 = pd.DataFrame({'team': ['A', 'A', 'A', 'A', 'B', 'B'],
                    'position': ['G', 'G', 'F', 'C', 'G', 'C'],
                    'points': [4, 4, 6, 8, 9, 5]})

#view DataFrame
print(df1)

  team position  points
0    A        G       4
1    A        G       4
2    A        F       6
3    A        C       8
4    B        G       9
5    B        C       5

#create second DataFrame
df2 = pd.DataFrame({'team': ['A', 'A', 'A', 'A', 'B', 'B'],
                    'rebounds': [12, 7, 8, 8, 5, 11]})

#view DataFrame
print(df2)

  team  rebounds
0    A        12
1    A         7
2    A         8
3    A         8
4    B         5
5    B        11

Example 1: Add Column from One DataFrame to Last Column Position in Another

The following code shows how to add the rebounds column from the second DataFrame to the last column position of the first DataFrame:

#add rebounds column from df2 to df1
df1['rebounds']= df2['rebounds']

#view updated DataFrame
print(df1)

  team position  points  rebounds
0    A        G       4        12
1    A        G       4         7
2    A        F       6         8
3    A        C       8         8
4    B        G       9         5
5    B        C       5        11

Notice that the rebounds column from the second DataFrame has been added to the last column position of the first DataFrame.

Example 2: Add Column from One DataFrame to Specific Column Position in Another

The following code shows how to add the rebounds column from the second DataFrame to the third column position of the first DataFrame:

#insert rebounds column from df2 into third column position of df1
df1.insert(2, 'rebounds', df2['rebounds'])

#view updated DataFrame
print(df1)

  team position  rebounds  points
0    A        G        12       4
1    A        G         7       4
2    A        F         8       6
3    A        C         8       8
4    B        G         5       9
5    B        C        11       5

Notice that the rebounds column from the second DataFrame has been added to the third column position of the first DataFrame.

x