4 回答
TA贡献1900条经验 获得超5个赞
首先,我们定义一个函数来根据您的关键字是否出现在给定句子中返回一个布尔值:
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]
TA贡献1942条经验 获得超3个赞
您可以使用 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)
TA贡献1891条经验 获得超3个赞
首先,我创建了一个系列,其中仅包含原始 `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"))]
TA贡献1887条经验 获得超5个赞
这里:
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)
添加回答
举报