我正在尝试删除数据中的停用词。所以它会从这个开始
data['text'].head(5)
Out[25]:
0 go until jurong point, crazy.. available only ...
1 ok lar... joking wif u oni...
2 free entry in 2 a wkly comp to win fa cup fina...
3 u dun say so early hor... u c already then say...
4 nah i don't think he goes to usf, he lives aro...
Name: text, dtype: object
对此
data['newt'].head(5)
Out[26]:
0 [go, jurong, point,, crazy.., available, bugis...
1 [ok, lar..., joking, wif, u, oni...]
2 [free, entry, 2, wkly, comp, win, fa, cup, fin...
3 [u, dun, say, early, hor..., u, c, already, sa...
4 [nah, think, goes, usf,, lives, around, though]
Name: newt, dtype: object
关于如何做到这一点,我有两种选择。我正在分别尝试这两个选项,所以它不会覆盖任何东西。首先,我将一个函数应用于数据列。这行得通,它消除了我想做的事情。
def process(data):
data = data.lower()
data = data.split()
data = [row for row in data if row not in stopwords]
return data
data['newt'] = data['text'].apply(process)
第二个选项不使用应用函数参数。它和函数一模一样,但为什么会返回TypeError: unhashable type: 'list'
?我检查if row not in stopwords
该行中的原因是什么,因为当我删除它时,它会运行但它不会删除停用词
data['newt'] = data['text'].str.lower()
data['newt'] = data['newt'].str.split()
data['newt'] = [row for row in data['newt'] if row not in stopwords]