如果前三个句子包含关键字,如何筛选字符串



我有一个名为df的熊猫数据帧。它有一个名为article的列。article列包含 600 个字符串,每个字符串代表一篇新闻文章。 我只想保留那些前四句话包含关键字"COVID-19"和("中国"或"中文"(的文章。但是我无法找到自己执行此操作的方法。

(在字符串中,句子用n分隔。示例文章如下所示:(

nChina may be past the worst of the COVID-19 pandemic, but they aren’t taking any chances.nWorkers in Wuhan in service-related jobs would have to take a coronavirus test this week, the government announced, proving they had a clean bill of health before they could leave the city, Reuters reported.nThe order will affect workers in security, nursing, education and other fields that come with high exposure to the general public, according to the edict, which came down from the country’s National Health Commission. .......

首先,我们定义一个函数,根据关键字是否出现在给定句子中来返回布尔值:

def contains_covid_kwds(sentence):
kw1 = 'COVID19'
kw2 = 'China'
kw3 = 'Chinese'
return kw1 in sentence and (kw2 in sentence or kw3 in sentence)

然后,我们通过将此函数(使用Series.apply(应用于df.article列的句子来创建布尔序列。

请注意,我们使用 lambda 函数来截断传递给contains_covid_kwds的句子,直到第五次出现'n',即您的前四个句子(有关其工作原理的更多信息,请点击此处(:

series = df.article.apply(lambda s: contains_covid_kwds(s[:s.replace('n', '#', 4).find('n')]))

然后我们将布尔序列传递给df.loc,以便将计算序列的行本地化True

filtered_df = df.loc[series]

您可以使用熊猫应用方法并按照我的方式进行操作。

string = "nChina may be past the worst of the COVID-19 pandemic, but they aren’t taking any chances.nWorkers in Wuhan in service-related jobs would have to take a coronavirus test this week, the government announced, proving they had a clean bill of health before they could leave the city, Reuters reported.nThe order will affect workers in security, nursing, education and other fields that come with high exposure to the general public, according to the edict, which came down from the country’s National Health Commission."
df = pd.DataFrame({'article':[string]})
def findKeys(string):
string_list = string.strip().lower().split('n')
flag=0
keywords=['china','covid-19','wuhan']
# Checking if the article has more than 4 sentences
if len(string_list)>4:
# iterating over string_list variable, which contains sentences.
for i in range(4):
# iterating over keywords list
for key in keywords:
# checking if the sentence contains any keyword
if key in string_list[i]:
flag=1
break
# Else block is executed when article has less than or equal to 4 sentences
else:
# Iterating over string_list variable, which contains sentences
for i in range(len(string_list)):
# iterating over keywords list
for key in keywords:
# Checking if sentence contains any keyword
if key in string_list[i]:
flag=1
break
if flag==0:
return False
else:
return True

然后在DF上调用熊猫应用方法:-

df['Contains Keywords?'] = df['article'].apply(findKeys)

首先,我创建一个系列,其中仅包含原始"df['articles']列中的前四个句子,并将其转换为小写,假设搜索应该与大小写无关。

articles = df['articles'].apply(lambda x: "n".join(x.split("n", maxsplit=4)[:4])).str.lower()

然后使用简单的布尔掩码仅筛选在前四个句子中找到关键字的那些行。

df[(articles.str.contains("covid")) & (articles.str.contains("chinese") | articles.str.contains("china"))]

在这里:

found = []
s1 = "hello"
s2 = "good"
s3 = "great"
for string in article:
if s1 in string and (s2 in string or s3 in string):
found.append(string)

最新更新