[英]Find 'matching' between words within one column and sentences in other columns
我有三个我想加入的数据集。 这些数据集有不同的长度(125、200、1000)
One Two Three
man | man and woman | there was a cat
nutella | lemon water | pancakes
bread | bread and nutella | look at you
glass | wine and water | table
我想将One
中的每个单词“连接”到包含该单词的Two
和Three
中的所有其他行,如下所示:
man : man and woman
nutella : bread and nutella
bread : bread and nutella
glass:
如果没有单词可以连接(例如glass
),我想将它们全部包含在一个名为'Other'
的新单词中。
您能否告诉我首先搜索(使用str.contains
或re.findall
)其他两列中的单词是否正确,然后使用zip
?
然而,我关心的是如何将One
中的每个单词与Two
和Three
中的其他单词联系起来。 我可能会手动进行(添加,作为搜索词,首先是man
,然后是nutella
等等),但我想知道是否可以自动进行(例如将第一列转换为列表)。
让我们尝试findall
并用melt
explode
,这里的玻璃是水滴,因为它不是基金
pat = '|'.join(r"\b{}\b".format(x) for x in df.One)
s = df.melt('One')
s['New'] = s.value.str.findall(pat)
s = s.explode('New')[['value', 'New']].dropna()
s
Out[42]:
value New
0 man and woman man
2 bread and nutella bread
2 bread and nutella nutella
把它放入字典
d = dict(zip(s.New,s.value))
Out[46]:
{'man': 'man and woman',
'bread': 'bread and nutella',
'nutella': 'bread and nutella'}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.