How do I add a column from one DataFrame to another in Pandas?

How do I add a column from one DataFrame to another in Pandas?

Adding a column from one DataFrame to another in Pandas is a simple process that allows for combining and organizing data from two separate DataFrames. This can be achieved by using the “merge” function and specifying the columns to merge on, or by using the “join” function and specifying the type of join to be performed. Both methods require matching values in the specified columns in order to successfully add the column from one DataFrame to another. This process is useful for consolidating data and creating a more comprehensive analysis.

Pandas: Add Column from One DataFrame to Another


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.

Additional Resources

The following tutorials explain how to perform other common tasks in pandas:

Cite this article

stats writer (2024). How do I add a column from one DataFrame to another in Pandas?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-do-i-add-a-column-from-one-dataframe-to-another-in-pandas/

stats writer. "How do I add a column from one DataFrame to another in Pandas?." PSYCHOLOGICAL SCALES, 28 Jun. 2024, https://scales.arabpsychology.com/stats/how-do-i-add-a-column-from-one-dataframe-to-another-in-pandas/.

stats writer. "How do I add a column from one DataFrame to another in Pandas?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-do-i-add-a-column-from-one-dataframe-to-another-in-pandas/.

stats writer (2024) 'How do I add a column from one DataFrame to another in Pandas?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-do-i-add-a-column-from-one-dataframe-to-another-in-pandas/.

[1] stats writer, "How do I add a column from one DataFrame to another in Pandas?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How do I add a column from one DataFrame to another in Pandas?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top