我有一個看起來像這樣的熊貓 df:
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]
和一個字典:
print(dict(list(csnps_indices.items())[0:2]))
{'10:100148542': [4582], '10:10091912': [4527]}
當我運行下面的命令時,
ld_r2_array_df["order"] = ld_r2_array_df.SNP_A.map(csnps_indices)
我收到此錯誤:
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'
我是一個完整的 python 新手,所以我想知道這里可能有什么問題。我已經嘗試將 dict 轉換為元組,但后來我收到了另一個錯誤:
TypeError: 'type' object is not subscriptable
請讓我知道我是否應該提供更多資訊來重現錯誤。謝謝!
uj5u.com熱心網友回復:
你的代碼似乎對我有用。
輸入資料:
>>> 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]}
嘗試映射:
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
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/348928.html
上一篇:使用以args作為排序鍵的方法時,如何使用Comparator和多個條件對串列進行排序?
下一篇:為什么在這里使用串列理解這么慢?
