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

我有一个名为df. 它有一个名为的列article。该article列包含 600 个字符串,每个字符串代表一篇新闻文章。我只想保留前四句包含关键字“COVID-19”AND(“中国”或“中文”)的文章。但是我无法找到一种方法来自己进行此操作。

(在字符串中,句子以 . 分隔\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.\ .......


qq_遁去的一_1
浏览 131回答 4
4回答

梵蒂冈之花

首先,我们定义一个函数来根据您的关键字是否出现在给定句子中返回一个布尔值: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]

手掌心

您可以使用 pandas apply 方法并按照我的方式进行操作。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 上调用 pandas apply 方法:-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"))]

慕工程0101907

这里: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)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python