我想构建一个新列,其中包含ai_functional列表中的单词在文本列中出现的次数的计数。
给出的列表是:
> ai_functional = ["natural language
> processing","nlp","A I ","Aritificial intelligence", "stemming","lemmatization","lemmatization","information
> extraction","text mining","text analytics","data-mining"]我想要的结果如下:
> text counter
>
> 1. More details A I Artificial Intelligence 2
> 2. NLP works very well these days 1
> 3. receiving information at the right time 1我一直使用的代码是
def func(stringans):
for x in ai_tech:
count = stringans.count(x)
return count
df['counter']=df['text'].apply(func)有没有人能帮帮我。我真的被卡住了,因为每次我应用它时,我在计数器列中得到的结果是0
发布于 2021-07-04 16:24:32
在执行count =时,擦除之前的值,希望对不同的计数求和
def func(stringans):
count = 0
for x in ai_tech:
count += stringans.count(x)
return count
# with sum and generator
def func(stringans):
return sum(stringans.count(x) for x in ai_tech)修复ai_tech中的一些拼写错误并将全部设置为.lower()将在计数器列中生成2,1,0,最后一行没有共同的值
import pandas as pd
ai_tech = ["natural language processing", "nlp", "A I ", "Artificial intelligence",
"stemming", "lemmatization", "information extraction",
"text mining", "text analytics", "data - mining"]
df = pd.DataFrame([["1. More details A I Artificial Intelligence"], ["2. NLP works very well these days"],
["3. receiving information at the right time"]], columns=["text"])
def func(stringans):
return sum(stringans.lower().count(x.lower()) for x in ai_tech)
df['counter'] = df['text'].apply(func)
print(df)
# ------------------
text counter
0 1. More details A I Artificial Intelligence 2
1 2. NLP works very well these days 1
2 3. receiving information at the right time 0https://stackoverflow.com/questions/68242870
复制相似问题