Pandas: How to Merge Two DataFrames with Different Column Names


You can use the following basic syntax to merge two pandas DataFrames with different column names:

pd.merge(df1, df2, left_on='left_column_name', right_on='right_column_name')

The following example shows how to use this syntax in practice.

Example: Merge Two Pandas DataFrames with Different Column Names

Suppose we have the following two pandas DataFrames:

import pandas as pd

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

#view DataFrame
print(df1)

  team  points
0    A       4
1    B       4
2    C       6
3    D       8
4    E       9
5    F       5

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

#view DataFrame
print(df2)

  team_name  rebounds
0         A        12
1         B         7
2         C         8
3         D         8
4         E         5
5         F        11

We can use the following syntax to perform an inner join, using the team column in the first DataFrame and the team_name column in the second DataFrame:

#merge DataFrames
df3 = pd.merge(df1, df2, left_on='team', right_on='team_name')

#view result
print(df3)

  team  points team_name  rebounds
0    A       4         A        12
1    B       4         B         7
2    C       6         C         8
3    D       8         D         8
4    E       9         E         5
5    F       5         F        11

Notice that we’re able to successfully perform an inner join even though the two column names that we used for the join were different in each DataFrame.

Note that we can also use the following code to drop the team_name column from the final merged DataFrame since the values in this column match those in the team column:

#drop team_name column
df3.drop('team_name', axis=1, inplace=True)

#view updated DataFrame
print(df3)

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

Notice that the team_name column has been dropped from the DataFrame.

Related: How to Drop Columns in Pandas (4 Examples)

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

Leave a Reply

Your email address will not be published. Required fields are marked *