【发布时间】:2021-09-19 12:51:24
【问题描述】:
我在数据框中有一列(相当长的)文本,对于每个文本,我想删除的句子索引列表。当我将文本拆分为句子时,Spacy 会生成句子索引。请考虑以下示例:
import pandas as pd
import spacy
nlp = spacy.load('en_core_web_sm')
data = {'text': ['I am A. I am 30 years old. I live in NY.','I am B. I am 25 years old. I live in SD.','I am C. I am 30 years old. I live in TX.'], 'todel': [[1, 2], [1], [1, 2]]}
df = pd.DataFrame(data)
def get_sentences(text):
text_clean = nlp(text)
sentences = text_clean.sents
sents_list = []
for sentence in sentences:
sents_list.append(str(sentence))
return sents_list
df['text'] = df['text'].apply(get_sentences)
print(df)
给出以下内容:
text todel
0 [I am A., I am 30 years old., I live in NY.] [1, 2]
1 [I am B. I am 25 years old., I live in SD.] [1]
2 [I am C. I am 30 years old., I live in TX.] [1, 2]
如果知道我有一个非常大的数据集,每行要删除 50 多个句子,您将如何有效地删除存储在 todel 中的句子?
我的预期输出是:
text todel
0 [I live in NY.] [1, 2]
1 [I am 25 years old., I live in SD.] [1]
2 [I live in TX.] [1, 2]
【问题讨论】:
-
你的预期输出是什么?
-
我在我的问题中补充了这一点
标签: python list dataframe apply spacy