检查文本列 pandas 中的停用词数量

use*_*396 3 python python-3.x pandas

如何检查文本列 pandas 中存在的停用词数量。我有一个巨大的数据集,因此非常感谢有效的方法。

from nltk.corpus import stopwords    
stop_words = set(stopwords.words('english'))

print(df)
      text                      
0  stackoverflow is good              
1  stackoverflow is not good     
Run Code Online (Sandbox Code Playgroud)

这是我想要的输出吗?

print(df)
      text                      number_of_stopwords
0  stackoverflow is good              1
1  stackoverflow is not good          2
Run Code Online (Sandbox Code Playgroud)

我尝试过类似下面的方法,但没有成功。

df.str.split().apply(lambda x: len(x in stop_words))
Run Code Online (Sandbox Code Playgroud)

jez*_*ael 5

使用集合的交集:

from nltk.corpus import stopwords    
stop_words = set(stopwords.words('english'))

df['n'] = df['text'].str.split().apply(lambda x: len(set(x) & stop_words))
Run Code Online (Sandbox Code Playgroud)

或者:

df['n'] = df['text'].apply(lambda x: len(set(x.split()) & stop_words))
Run Code Online (Sandbox Code Playgroud)
print (df)
                        text  n
0      stackoverflow is good  1
1  stackoverflow is not good  2
Run Code Online (Sandbox Code Playgroud)