I have a pandas df which looks like this:
ld_r2_array_df.head()
SNP_A r2_ld_array
0 1:203337500 [1, NONE, NONE, NONE, NONE]
1 20:6730358 [1, NONE, NONE, NONE, NONE]
2 7:50878411 [1, NONE, NONE, NONE, NONE]
3 20:47025897 [1, NONE, NONE, NONE, NONE]
4 18:7553626 [1, NONE, NONE, NONE, NONE]
And a dict:
print(dict(list(csnps_indices.items())[0:2]))
{'10:100148542': [4582], '10:10091912': [4527]}
When I run the command below,
ld_r2_array_df["order"] = ld_r2_array_df.SNP_A.map(csnps_indices)
I receive this error:
Traceback (most recent call last):
File "pandas/_libs/hashtable_class_helper.pxi", line 4588, in pandas._libs.hashtable.PyObjectHashTable.map_locations
TypeError: unhashable type: 'list'
Exception ignored in: 'pandas._libs.index.IndexEngine._call_map_locations'
I am a complete python newbie, so I was wondering what might be the issue here. I already tried converting the dict to tuple, but then I received another error:
TypeError: 'type' object is not subscriptable
Please let me know if I should provide further info for reproduction of the error. Thanks!
CodePudding user response:
Your code seems to work for me.
Input data:
>>> df
SNP_A r2_ld_array
0 1:203337500 [1, NONE, NONE, NONE, NONE]
1 20:6730358 [1, NONE, NONE, NONE, NONE]
2 7:50878411 [1, NONE, NONE, NONE, NONE]
3 20:47025897 [1, NONE, NONE, NONE, NONE]
4 18:7553626 [1, NONE, NONE, NONE, NONE]
>>> d
{'1:203337500': [4582], '7:50878411': [4527]}
Try to map:
df['order'] = df['SNP_A'].map(d)
print(df)
# Output:
SNP_A r2_ld_array order
0 1:203337500 [1, NONE, NONE, NONE, NONE] [4582]
1 20:6730358 [1, NONE, NONE, NONE, NONE] NaN
2 7:50878411 [1, NONE, NONE, NONE, NONE] [4527]
3 20:47025897 [1, NONE, NONE, NONE, NONE] NaN
4 18:7553626 [1, NONE, NONE, NONE, NONE] NaN