Home » Pandas: Solve ‘You are trying to merge on object and int64 columns’

Pandas: Solve ‘You are trying to merge on object and int64 columns’

You are trying to merge on object and int64 columns
Want to do a random act of kindness? Share this post.

Pandas is the go-to package for anything data science in Python. However, if you’re used to R and the convenience of dplyr or data.table, pandas can be a real pain in the ###, sometimes.

For example, the following error is a real newb issue.

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

It can occur in two scenarios:

  1. using the join method: you are probably joining DataFrames on labels and not on indices
  2. using the merge method: you are probably joining DataFrames on two columns that are not of the same type

You are trying to join on labels and not on indices using the join method

Example:

data_x.join(data_y, on='key')

In the first scenario, you can edit your code to join on the index. In the following code, I set the index on the columns I want to join.

data_x.set_index('key').join(data_y.set_index('key'))

But what might even be more simple, is replacing the join method with the merge method.

data_x.merge(data_y)

You are joining on columns of different types using the merge method

Example:

data_x.merge(data_y, on='key')

In this second scenario, you can simply change the column type of one of the columns — or both. A convenient way is through the astype method.

data_x.key.astype(int)
data_y.key.astype(int)
data_x.merge(data_y, on='key')

Great succes!

Say thanks, ask questions or give feedback

Technologies get updated, syntax changes and honestly… I make mistakes too. If something is incorrect, incomplete or doesn’t work, let me know in the comments below and help thousands of visitors.

Want to do a random act of kindness? Share this post.

1 thought on “Pandas: Solve ‘You are trying to merge on object and int64 columns’”

  1. I’m not sure where you’re getting your іnfo, but great topic.
    I needs to spend some time learning more or understanding more.
    Thanks for excellеnt info I was looking for this info for my missіon.

Leave a Reply

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