程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了删除熊猫数据集中的停用词大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决删除熊猫数据集中的停用词?

开发过程中遇到删除熊猫数据集中的停用词的问题如何解决?下面主要结合日常开发的经验,给出你关于删除熊猫数据集中的停用词的解决方法建议,希望对你解决删除熊猫数据集中的停用词有所启发或帮助;

我正在尝试删除 apadas 数据集中的停用词,其中每一行都有一个词的标记化列表, 单词列表的格式如下:

['Uno',','dos','One','two','tres','quatro','Yes','Wooly','Bully','Watch','it','Now','watch','Here','he','come','here','git','ya','Matty','told','HattIE','about','a','thing','she','saw','Had','big','horns','and','wooly','jaw','yes','drive','``','Let',"'s",'do',"n't",'take','no','chance','not','be','L-seven','learn','to','dance',"''",'Yeah','That','the','Get','you','someone','really','pull','wool','with','You','got','it']

使用以下代码执行此操作。
ret = df['tokenized_lyric'].apply(lambda x: [item for item in x if item.lower() not in stops])

print(ret)

这让我得到如下列表

e0       [n,n,e,w,r,...
2165    [,l,p,...

似乎删除了几乎所有字符。 我如何让它只删除我设置的停用词?

解决方法

您正在使用列表推导式迭代字符串的字符。相反,在 lower() 之后,使用 split() 拆分字符串,然后迭代工作令牌,如下所示 -

print([i for i in 'hi there']) #iteraTing over the characters
print([i for i in 'hi there'.split()]) #iteraTing over the words

['h','i',' ','t','h','e','r','e']
['hi','there']

试试这个 lambda 函数 -

s = 'Hello World And Underworld'

stops = ['and','or','the']

f = lambda x: [item for item in x.split() if item.lower() not in stops]
f(s)
['Hello','world','underworld']

W.r.t 你的代码,它会是 -

df['tokenized_lyric'].apply(lambda x: [item for item in x.split() if item.lower() not in stops])
,
from nltk.corpus import stopwords

# stop words from nltk library
stopwords = stopwords.words('english')

# user defined stop words
custom_stopwords = ['hey','Hello'] 

# complete list of stop words
complete_stopwords = stopwords + custom_stopwords

# 
df['lyrics_clean'] = df['lyrics'].apply(lambda x: [word for word in x.split() if word not in (complete_stopwords)])

大佬总结

以上是大佬教程为你收集整理的删除熊猫数据集中的停用词全部内容,希望文章能够帮你解决删除熊猫数据集中的停用词所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。