Workaround for pandas merge error "unhashable type: dict"

Viewed 1125

i have 2 data frames ds and dk and want to merge that with a common column result using the merge command:

result = pd.merge(ds,dk,on='result')

but the result column is actually a dictionary and resulting in the error:

 "unhashable type: dict"

what is the possible solution for merging these frames? Can the result column be changed to a string type and then be merged along the column? I tried using dk['result']= str(dk['result']) and ds['result']= str(ds['result']) to convert then and merge but it did not work.

Thanks

1 Answers
ds = pd.DataFrame({'result':[{'a':1,'b':1},{'a':3,'b':9},{'a':7,'b':5}],
                   'a':[0,1,0]})

dk = pd.DataFrame({'result':[{'a':1,'b':1},{'a':3,'b':9},{'a':7,'b':5}],
                   'b':[2,2,2]})

ds['result'] = ds['result'].astype(str) # transorm dict to str
dk['result'] = dk['result'].astype(str) # transorm dict to str

result = pd.merge(ds,dk,on='result')
result['result'] = result['result'].apply(eval) # comeback im dict format
Related