我正在尝试使用新的词干字符串值重新分配系列中列表的值。但是,我不知道该怎么做。目前我正在制作一个新列表并附加新词干词,但这只是将所有列表中的所有词放在列表 ns 中。我只想用新词干的项目更新当前列表。
words = data.String.apply(lambda x: word_tokenize(x))
ns =[]
#print(words)
for i in words:
for j in i:
ns.append(ps.stem(j))
例如,单词=
0 [I, loved, dogs, because, they, are, cute, and...
1 [my, dog, is, looking, at, me, weird, maybe, c...
2 [I, think, I, look, like, a, cupacake, one, wi...
3 [do, you, want, to, be, a, snowman, no, thanks...
4 [hey, do, you, know, what, time, it, is, cooking,...
5 [dogs, are, so, awesome, dogs, are, so, awesome]
通过 for 循环来阻止单词后,words 应该是这样的:
0 [I, love, dog, becaus, they, are, cute, and...
1 [my, dog, is, look, at, me, weird, maybe, c...
2 [I, think, I, look, like, a, cupacake, one, wi...
3 [do, you, want, to, be, a, snowman, no, thank...
4 [hey, do, you, know, what, time, it, is, cook,...
5 [dog, are, so, awesome, dog, are, so, awesome]
在:
print(type(words))
print(type(words[1]))
print(type(words[1][1]))
输出:
<class 'pandas.core.series.Series'>
<class 'list'>
<class 'str'>
有什么想法吗?
谢谢!
请您参考如下方法:
通过 ps.stem 函数使用列表理解:
print (data)
String
0 I loved dogs because they are cute and
1 my dog is looking at me weird maybe
2 I think I look like a cupacake one
3 do you want to be a snowman no thanks
4 hey do you know what time it is cooking
5 dogs are so awesome dogs are so awesome
from nltk.stem.snowball import SnowballStemmer
from nltk import word_tokenize
ps = SnowballStemmer("english")
words = data.String.apply(lambda x: [ps.stem(y) for y in word_tokenize(x)])
print (words)
0 [i, love, dog, becaus, they, are, cute, and]
1 [my, dog, is, look, at, me, weird, mayb]
2 [i, think, i, look, like, a, cupacak, one]
3 [do, you, want, to, be, a, snowman, no, thank]
4 [hey, do, you, know, what, time, it, is, cook]
5 [dog, are, so, awesom, dog, are, so, awesom]
Name: String, dtype: object
如果需要重新分配到同一列:
data.String = data.String.apply(lambda x: [ps.stem(y) for y in word_tokenize(x)])
print (data)
String
0 [i, love, dog, becaus, they, are, cute, and]
1 [my, dog, is, look, at, me, weird, mayb]
2 [i, think, i, look, like, a, cupacak, one]
3 [do, you, want, to, be, a, snowman, no, thank]
4 [hey, do, you, know, what, time, it, is, cook]
5 [dog, are, so, awesom, dog, are, so, awesom]
或到新列:
data['Stem'] = data.String.apply(lambda x: [ps.stem(y) for y in word_tokenize(x)])
