Help with Python Pandas (pd.merge)

Hello community,

I am hoping someone can guide me in the right direction regarding Python Pandas merge function. I have 2 csv files I am trying to join. The first csv will only contain 1 ID that will constantly change. I need to take that ID and search the 2nd file and return the values in the adjacent cells. I can easily do this if my data on both files are strings. However, I will be working with alphanumeric values. Pandas does not like that. Is there a work around? This is what I have as an example:

df1 contains the following

df2 contains the following
External_ID, Sample_ID
1587966543, 025-85-258
1659787846, 068-87-856
A569787522, 568-98-785
A158976453, 485-89-562

My code:
import pandas as pd
df1 = pd.read_csv(‘BarcodeScan.csv’)
df2 = pd.read_csv(‘SamplePrepIDs.csv’)

df3 = pd.merge(df1, df2[[‘External_ID’, ‘Sample_ID’]], on=‘External_ID’, how=‘left’)

The result I’m looking for is:
External_ID, Sample_ID
A158976453, 485-89-562

The error I’m receiving is that “you are trying to merge on int64 and object columns. If you wish to proceed you should use pd.concat.”
Concat is not what I need. Is there a work around or an entirely other method that I need to look at?

Thank you for all the help in advance!

Welcome to the forums!
Did you do a to check the dtypes for the dataframes?

I think your best bet is that you have to change the dtype of one of the columns. I would change the dtype on the col that is alpha-numeric.

Try this:


Thank you so much! That link you sent me worked perfectly. I originally tried to change the dtype but realized that my code was off. The code I found from the link you provided worked perfectly to change the dtype. Thank you so much! I really appreciate your help! Merry Christmas!

1 Like

Yay!! I’m glad it worked out! :partying_face:
You’re welcome :slightly_smiling_face:
Merry Christmas to you as well.