【发布时间】:2015-11-16 11:31:28
【问题描述】:
我有一个如下所示的数据框:
id points
a [c,v,b,n]
b []
c [x,a]
....
和一个字典(我也有它作为数据框):
{'a': ['j','c'],
'b': [p,r,q]
'c': [n,k,l,x,a]
....}
我想搜索是否包含字典的键是数据帧的点,然后从字典点中删除字典中没有匹配的项目。预期输出:
id points
a [c]
b []
c [x,a]
我试过了
for key,point in my_dict.items():
if df['points'].str.contains(point).any()
但我得到TypeError: unhashable type: 'list'
我尝试将数据框转换为字典,但搜索时间太长,因为我需要更多 for 循环。对代码或数据结构改进有何建议?
编辑
数据的另一种表示:
id points
a [c,v,b,n]
b []
c [x,a]
....
和
points
j,c
p,r,q
n,k,l,x,a
【问题讨论】:
标签: python dictionary pandas dataframe