Fixing Python and Pandas problem when Trying to merge 2 dataframes but get ValueError

Python and Pandas problem when Trying to merge 2 dataframes but get ValueError is an error which occurs when at least one element of the first column you are trying to merge has a different data type form the corresponding element of the second column.

Today, I explain why this error occurs and how to get rid of it, while also trying to add other possible solutions that could help you remove the error for good.

Exploring the issue : Fixing Python and Pandas problem when Trying to merge 2 dataframes but get ValueError

This is an error which occurs when at least one element of the first column you are trying to merge has a different data type form the corresponding element of the second column.

Your error message should match the one bellow. We want to avoid confusion by not mixing between error messages.

                                                                       #
ValueError: You are trying to merge on object and int64 columns. If you wish to proceed you should use pd.concat
                                                                       #

Bellow is a number of tested solutions that I have tried and worked for me.

Solution 1 : Convert the data before merging using astype(string) or astype(Int64) etc. Depending on your desired data type.

Fist, we should understand the error really well, the error occurs when at least one element of the first column you want to merge has a different data type form the corresponding element of the second column.

For instance, you are trying to merge two tables. The first table has birthday which is an int while in the second table the corresponding element is a string.

That is why you have the error, we cannot merge two elements who do not share a common data type.

The first solution is to first convert the data types before merging. For example, you can use astype(string) in order to convert the data to string. You can do that in two ways.

In order to convert the name element of the table df1 to string we can use the line bellow.

                                                                       #
df1.name.astype(string) 
                                                                       #

An other way to convert the name element of the table df2 to string.

                                                                       #
df2['name']=df2['name'].astype(string)
                                                                       #

The first solution is really the universal one and should fix the error for most people. If you are one of the unlucky ones try the solution bellow.

Solution 2 : use .set_index(‘key’) or use .merge() instead of using .join() to merge the two dataframes

For a minority of people the error occurs when you try to merge columns that are not the index.

The solution is to set key to be the index, you can do that using the following

                                                                       #
df.set_index('key').join(other.set_index('key'))
                                                                       #

Another workaround is to use .merge() instead of using .join() when trying to do the merge.

The solution above should be enough to cover the error in most environments, I wish you good luck with your projects.

Summing-up : 

Guys, this has been my best attempt at helping you understand and solve this issue. I hope you found a solution which suits your needs. Consider helping the blog if you can by donating to our Kofi account.

Thank you for reading, keep coding and cheers. If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/