2.4 C
London
Friday, December 20, 2024
HomePandas in PythonGeneral Functions in PythonPandas: How to Add Column from One DataFrame to Another

Pandas: How to Add Column from One DataFrame to Another

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

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:

How to Change the Order of Columns in Pandas
How to Rename Columns in Pandas
How to Sort Columns by Name in Pandas

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories