我有一个这样的单词列表:
words1 = ['hi','my']
words2 = ['name','is']
我有 Dataframe df
这样地:
id Sentence
0 'my name was'
1 'hi i am'
2 'my phone is'
3 'what is this'
4 'her name was'
我正在运行下面的代码,以获取值匹配的 Dataframe 索引。
matched_idx1 = df.loc[df.Sentence.str.contains('|'.join(words1)),:].index.array
matched_idx2 = df.loc[df.Sentence.str.contains('|'.join(words2)),:].index.array
因此 matched_idx1
给出了数组:
[0,1,2]
及 matched_idx2
给出了数组:
[0,2,3,4]
现在,我想获得在contains函数中匹配的值的列表或数组。
比如说一个新的变量 matched_idx1_values
输出应为:
['my','hi','my']
以及 matched_idx2_values
输出应为:
['name','is','is','name']
请让我知道如何获得这些索引以及它们匹配的值。这个例子很简单,我的列表中还有很多词。
谢谢
1条答案
按热度按时间qacovj5a1#
下面是使用spacy的完整示例: