如果 pandas 数据框中存在带有索引值的句子,则尝试 return 行

Trying to return the row if sentence is present in pandas dataframe with index value

我有一个数据框。我正在实施句子转换器,并根据搜索查询 returning 一行。例如

search_string = "thor's weapon"
search_vect = model.encode([search_string])
K = 3 # no. of paragraphs that has to be extracted
distilbert_similar_indexes = find_similar(search_vect, embeddings_distilbert, K)
print(distilbert_similar_indexes)

输出为:

array([7, 6, 1], dtype=int64)

代码后跟:

output_data = []
for index in distilbert_similar_indexes:
    output_data.append(paragraph[index])
print(output_data[0])

输出为:

'Stormbreaker is an enchanted axe used by Thor. It was forged from Uru on Nidavellir, and can summon the Bifrost.'

现在我想从给定的数据框中搜索上面的句子,并希望 return 包含该句子和所有给定句子的单行。例如,如果我有像

这样的数据框
Sentences                                                Tag
0   Thor Odinson is the Asgardian God of Thunder, ...    Excel 1
1   Upon being welcomed back to Asgard as a hero, ...    Excel 1
2   Thor returned to Asgard having defeated his br...    Excel 2
3   Loki Laufeyson was the biological son of Laufe...    Excel 1
4   Stormbreaker is an enchanted axe used by Thor....    Excel 3

现在我想return下面一行

4   Stormbreaker is an enchanted axe used by Thor....    Excel 3

你可以试试这个:

slice = df[df["Sentences"].str.contains(output_data[0])]

print(slice)
# Outputs
4   Stormbreaker is an enchanted axe used by Thor....    Excel 3