When the names are different, use the xxx_on parameters instead of on=:

pd.merge(df1, df2, left_on=  ['userid', 'column1'],
                   right_on= ['username', 'column1'], 
                   how = 'left')
Answer from Zeugma on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ pandas โ€บ merge-join-two-dataframes-on-multiple-columns-in-pandas
Merge/Join Two Dataframes on Multiple Columns in Pandas - GeeksforGeeks
July 23, 2025 - Explanation: The suffixes=('_df1', '_df2') ensures overlapping column names are differentiated aqnd this prevents confusion when both DataFrames have columns with identical names.
๐ŸŒ
Statology
statology.org โ€บ home โ€บ pandas: how to merge two dataframes with different column names
Pandas: How to Merge Two DataFrames with Different Column Names
May 10, 2022 - 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. Suppose we have the following two pandas DataFrames:
Discussions

python - Pandas join on columns with different names - Stack Overflow
I have two different data frames that I want to perform some sql operations on. Unfortunately, as is the case with the data I'm working with, the spelling is often different. See the below as an More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - pandas: merge (join) two data frames on multiple columns - Stack Overflow
As can be seen from the above example, if the merge keys have different names, all keys will show up as their individual columns in the merged dataframe. In the example above, in the top dataframe, A_col1 and B_col1 are identical and A_col2 and B_col2 are identical. More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - Joining pandas DataFrames by Column names - Stack Overflow
I have two DataFrames with the following column names: frame_1: event_id, date, time, county_ID frame_2: countyid, state I would like to get a DataFrame with the following columns by left-joining... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Pandas merge multiple dataframes with different columns
Do you mean merge or concatenate? They're two different things. Your concatenation code stacks the dataframes either side-by-side or one on top of the other, depending on your axis argument. Merging means you're taking a common column in the dataframes and putting rows together based on the column values. I cannot replicate your issue, as illustrated by my toy code below, so it you be helpful if you posted a minimal reproducible example . df1 = pandas.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]}) df2 = pandas.DataFrame({"a": [4, 5, 6], "c": [7, 8, 9]}) df3 = pandas.DataFrame({"b": [11, 12, 13], "d": [2, 2, 2]}) d = {1: df1, 2: df2, 3: df3} df = pandas.concat(d.values()) Gives: a b c d 0 1.0 4.0 NaN NaN 1 2.0 5.0 NaN NaN 2 3.0 6.0 NaN NaN 0 4.0 NaN 7.0 NaN 1 5.0 NaN 8.0 NaN 2 6.0 NaN 9.0 NaN 0 NaN 11.0 NaN 2.0 1 NaN 12.0 NaN 2.0 2 NaN 13.0 NaN 2.0 More on reddit.com
๐ŸŒ r/learnpython
7
21
December 16, 2022
๐ŸŒ
w3resource
w3resource.com โ€บ python-exercises โ€บ pandas โ€บ pandas-merge-dataframes-on-multiple-columns-with-different-names.php
Pandas - Merge DataFrames on multiple columns with different names
May 6, 2025 - Learn how to merge DataFrames on multiple columns where the column names differ in each DataFrame using pd.merge() with left_on and right_on.
๐ŸŒ
Pandas
pandas.pydata.org โ€บ docs โ€บ reference โ€บ api โ€บ pandas.merge.html
pandas.merge โ€” pandas 3.0.1 documentation
Since pandas 3.0, this method always returns a new object using a lazy copy mechanism that defers copies until necessary (Copy-on-Write). See the user guide on Copy-on-Write for more details. ... If True, adds a column to the output DataFrame called โ€œ_mergeโ€ with information on the source of each row. The column can be given a different name ...
๐ŸŒ
Pandas
pandas.pydata.org โ€บ docs โ€บ user_guide โ€บ merging.html
Merge, join, concatenate and compare โ€” pandas 3.0.2 documentation
The merge suffixes argument takes a tuple or list of strings to append to overlapping column names in the input DataFrame to disambiguate the result columns:
Top answer
1 of 2
50

When the names are different, use the xxx_on parameters instead of on=:

pd.merge(df1, df2, left_on=  ['userid', 'column1'],
                   right_on= ['username', 'column1'], 
                   how = 'left')
2 of 2
6

An alternative approach is to use join setting the index of the right hand side DataFrame to the columns ['username', 'column1']:

df1.join(df2.set_index(['username', 'column1']), on=['userid', 'column1'], how='left')

The output of this join merges the matched keys from the two differently named key columns, userid and username, into a single column named after the key column of df1, userid; whereas the output of the merge maintains the two as separate columns. To illustrate, consider the following example:

import numpy as np
import pandas as pd

df1 = pd.DataFrame({'ID': [1,2,3,4,5,6], 'pID' : [21,22,23,24,25,26], 'Values' : [435,33,45,np.nan,np.nan,12]})
##    ID  Values  pID
## 0   1   435.0   21
## 1   2    33.0   22
## 2   3    45.0   23
## 3   4     NaN   24
## 4   5     NaN   25
## 5   6    12.0   26

df2 = pd.DataFrame({'ID' : [4,4,5], 'pid' : [24,25,25], 'Values' : [544, 545, 676]})
##    ID  Values  pid
## 0   4     544   24
## 1   4     545   25
## 2   5     676   25

pd.merge(df1, df2, how='left', left_on=['ID', 'pID'], right_on=['ID', 'pid']))
##    ID  Values_x  pID  Values_y   pid
## 0   1     435.0   21       NaN   NaN
## 1   2      33.0   22       NaN   NaN
## 2   3      45.0   23       NaN   NaN
## 3   4       NaN   24     544.0  24.0
## 4   5       NaN   25     676.0  25.0
## 5   6      12.0   26       NaN   NaN

df1.join(df2.set_index(['ID','pid']), how='left', on=['ID','pID'], lsuffix='_x', rsuffix='_y'))
##    ID  Values_x  pID  Values_y
## 0   1     435.0   21       NaN
## 1   2      33.0   22       NaN
## 2   3      45.0   23       NaN
## 3   4       NaN   24     544.0
## 4   5       NaN   25     676.0
## 5   6      12.0   26       NaN

Here, we also need to specify lsuffix and rsuffix in join to distinguish the overlapping column Value in the output. As one can see, the output of merge contains the extra pid column from the right hand side DataFrame, which IMHO is unnecessary given the context of the merge. Note also that the dtype for the pid column has changed to float64, which results from upcasting due to the NaNs introduced from the unmatched rows.

This aesthetic output is gained at a cost in performance as the call to set_index on the right hand side DataFrame incurs some overhead. However, a quick and dirty profile shows that this is not too horrible, roughly 30%, which may be worth it:

sz = 1000000 # one million rows
df1 = pd.DataFrame({'ID': np.arange(sz), 'pID' : np.arange(0,2*sz,2), 'Values' : np.random.random(sz)})
df2 = pd.DataFrame({'ID': np.concatenate([np.arange(sz/2),np.arange(sz/2)]), 'pid' : np.arange(0,2*sz,2), 'Values' : np.random.random(sz)})

%timeit pd.merge(df1, df2, how='left', left_on=['ID', 'pID'], right_on=['ID', 'pid'])
## 818 ms ยฑ 33.4 ms per loop (mean ยฑ std. dev. of 7 runs, 1 loop each)

%timeit df1.join(df2.set_index(['ID','pid']), how='left', on=['ID','pID'], lsuffix='_x', rsuffix='_y')
## 1.04 s ยฑ 18.2 ms per loop (mean ยฑ std. dev. of 7 runs, 1 loop each)
Top answer
1 of 6
637

Try this

new_df = pd.merge(
    left=A_df, 
    right=B_df,
    how='left',
    left_on=['A_c1', 'c2'],
    right_on=['B_c1', 'c2'],
)

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.merge.html

left_on : label or list, or array-like Field names to join on in left DataFrame. Can be a vector or list of vectors of the length of the DataFrame to use a particular vector as the join key instead of columns

right_on : label or list, or array-like Field names to join on in right DataFrame or vector/list of vectors per left_on docs

2 of 6
28
  1. It merges according to the ordering of left_on and right_on, i.e., the i-th element of left_on will match with the i-th of right_on.

    In the example below, the code on the top matches A_col1 with B_col1 and A_col2 with B_col2, while the code on the bottom matches A_col1 with B_col2 and A_col2 with B_col1. Evidently, the results are different.

  2. As can be seen from the above example, if the merge keys have different names, all keys will show up as their individual columns in the merged dataframe. In the example above, in the top dataframe, A_col1 and B_col1 are identical and A_col2 and B_col2 are identical. In the bottom dataframe, A_col1 and B_col2 are identical and A_col2 and B_col1 are identical. Since these are duplicate columns, they are most likely not needed. One way to not have this problem from the beginning is to make the merge keys identical from the beginning. See bullet point #3 below.

  3. If left_on and right_on are the same col1 and col2, we can use on=['col1', 'col2']. In this case, no merge keys are duplicated.

    df1.merge(df2, on=['col1', 'col2'])
    

  4. You can also merge one side on column names and the other side on index too. For example, in the example below, df1's columns are matched with df2's indices. If the indices are named, as in the example below, you can reference them by name but if not, you can also use right_index=True (or left_index=True if the left dataframe is the one being merged on index).

    df1.merge(df2, left_on=['A_col1', 'A_col2'], right_index=True)
    # or
    df1.merge(df2, left_on=['A_col1', 'A_col2'], right_on=['B_col1', 'B_col2'])
    

  5. By using the how= parameter, you can perform LEFT JOIN (how='left'), FULL OUTER JOIN (how='outer') and RIGHT JOIN (how='right') as well. The default is INNER JOIN (how='inner') as in the examples above.

  6. If you have more than 2 dataframes to merge and the merge keys are the same across all of them, then join method is more efficient than merge because you can pass a list of dataframes and join on indices. Note that the index names are the same across all dataframes in the example below (col1 and col2). Note that the indices don't have to have names; if the indices don't have names, then the number of the multi-indices must match (in the case below there are 2 multi-indices). Again, as in bullet point #1, the match occurs according to the ordering of the indices.

    df1.join([df2, df3], how='inner').reset_index()
    

Find elsewhere
๐ŸŒ
Saturn Cloud
saturncloud.io โ€บ blog โ€บ how-to-merge-pandas-dataframes-with-different-column-names-and-avoid-duplicates
How to Merge Pandas DataFrames with Different Column Names and Avoid Duplicates | Saturn Cloud Blog
December 19, 2023 - Pandas is a powerful tool for data manipulation and analysis, but merging DataFrames can be tricky, especially when the columns have different names. In this article, we will explore how to merge Pandas DataFrames with different column names and avoid duplicates.
๐ŸŒ
Saturn Cloud
saturncloud.io โ€บ blog โ€บ how-to-perform-a-pandas-join-on-columns-with-different-names
How to Perform a Pandas Join on Columns with Different Names | Saturn Cloud Blog
November 14, 2023 - Use the left_on and right_on parameters of the merge() or join() function to specify the columns to use from the left and right DataFrames, respectively. Perform the join and create a new DataFrame that contains the joined data.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ pandas merge multiple dataframes with different columns
r/learnpython on Reddit: Pandas merge multiple dataframes with different columns
December 16, 2022 -

Using pandas, I'm trying to merge approx 150 dataframes from a dictionary into one dataframe. Not all of the files have the same number of columns. 90% of the column names are the same. My intention is to have Null/NaN where the data is absent for a given dataframe.

I tried examples shown here: https://stackoverflow.com/questions/28097222/pandas-merge-two-dataframes-with-different-columns and here: https://www.geeksforgeeks.org/pandas-merge-two-dataframes-with-different-columns/

df = pd.concat(d.values(), axis=0, ignore_index=True)

df = pd.concat(d.values(), ignore_index=True, sort = False)

But I keep getting the error below. Any help would be appreciated.

pandas.errors.ParserError: Error tokenizing data. C error: Expected 50 fields in line 5, saw 51

๐ŸŒ
Dasboardai
dasboardai.com โ€บ blog โ€บ pandas-merge-when-column-names-are-different-a-beginners-guide
Pandas Merge When Column Names Are Different
merged_df = pd.merge(left_df, right_df, on='common_column_name', how='inner') ... In left_df, the column is called customer_id. In right_df, the same column is called client_id. We can't use on='common_column_name' because the names donโ€™t match. Here's where the left_on and right_on parameters ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ pandas-merge-two-dataframes-with-different-columns
Pandas - Merge two dataframes with different columns - GeeksforGeeks
July 23, 2025 - In Pandas, you can merge two DataFrames with different columns using concat(), merge() and join(). concat() method is ideal for combining multiple DataFrames vertically (adding rows) or horizontally (adding columns) without requiring a key column ...
๐ŸŒ
KDnuggets
kdnuggets.com โ€บ 2023 โ€บ 01 โ€บ merge-pandas-dataframes.html
How to Merge Pandas DataFrames - KDnuggets
In the example above, we change ... the right_on for the second DataFrame. There are five different types merged in the Pandas merge method....
๐ŸŒ
Pandas
pandas.pydata.org โ€บ docs โ€บ reference โ€บ api โ€บ pandas.DataFrame.merge.html
pandas.DataFrame.merge โ€” pandas 3.0.2 documentation
Since pandas 3.0, this method always ... โ€œ_mergeโ€ with information on the source of each row. The column can be given a different name by providing a string argument....
๐ŸŒ
Data Science Parichay
datascienceparichay.com โ€บ home โ€บ blog โ€บ pandas โ€“ merge dataframes on multiple columns
Pandas - Merge DataFrames on Multiple Columns - Data Science Parichay
January 4, 2021 - The following is the syntax: df_merged ... the column names are different in the two dataframes, use the left_on and right_on parameters to pass your column lists to merge on....
Top answer
1 of 2
1

pandas.merge() is a class function orientated to produce joins of Databases with primary keys and foreign keys as in SQL Style databases. See Difference Between Primary and Foreign Key.

The problem here is that you are trying to introduce values of different dtypes (use df.dtypes to see the types of all columns in your DataFrames) to an existing column. That happens because pandas takes the left DataFrame assigned in the function as the "base", and tries to add new records to it, since the dtype is different, it causes an error.

In fact, the documentation is more likely to appear as a pd.DataFrame method, because it is behaved as a (say) "Mother DataFrame that receives new rows". See documentation pd.DataFrame.merge

The error also recommends to use the pandas.concat method, since it sees that the dtypes are different and thinks you may are willing to just join two DataFrames. Which can be preferible, if there are no existing records that have the same TrackName and Artist (for example), in that case you would like to join them with a concat, because there is no additional information you can gain about a record using another DataFrame.

My recommendation is: rename columns in DataFrame 2019 as they are in DataFrame 2018, with the same name if they refer to the same attribute, you can use pd.DataFrame.rename, then, change the dtype of the columns that you will like to do the merge on and make sure they are the same. Finally, try to do an Outer Join with the merge function, using the Song Name, for example. You will see if there are matches or see that all records may be different databases.

2 of 2
0

So you are not able to merge on ID as ID is of object datatype in one table and int in other table:

df_2018.dtypes

id                   object
name                 object
artists              object

df_2019.dtypes

ID                 int64
TrackName         object
ArtistName        object

Now I tried merging two tables on 'name' and 'artists' and I was able to do that. Here is the code:

new_df = pd.merge(df_2018, df_2019, left_on=['name','artists'], right_on = ['TrackName','ArtistName'])

new_df.columns

Index(['id', 'name', 'artists', 'danceability', 'energy', 'key', 'loudness',
       'mode', 'speechiness', 'acousticness', 'instrumentalness', 'liveness',
       'valence', 'tempo', 'duration_ms', 'time_signature', 'ID', 'TrackName',
       'ArtistName', 'Genre', 'BeatsPerMinute', 'Energy', 'Danceability',
       'LoudnessdB', 'Liveness', 'Valence', 'Length', 'Acousticness',
       'Speechiness', 'Popularity'],
      dtype='object')

I could get all the columns as desired. Let me know if you are still facing any issues. Do share columns for which you are facing an issue

๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ pandas โ€บ pandas-merge-dataframe
Pandas Merge Dataframe - GeeksforGeeks
July 23, 2025 - You can refer this article for ... on have different names in the two DataFrames, you can use the left_on and right_on parameters to specify the columns to merge on....
๐ŸŒ
w3resource
w3resource.com โ€บ python-exercises โ€บ pandas โ€บ pandas-merge-dataframes-with-different-join-column-names.php
Pandas - Merge DataFrames with different join column names
May 6, 2025 - Learn how to merge two DataFrames with different column names for the join using pd.merge() with left_on and right_on.