【问题标题】:reassign string values in a list in a series在一系列列表中重新分配字符串值
【发布时间】:2019-08-19 09:59:08
【问题描述】:

我正在尝试使用新的词干字符串值重新分配系列中的列表值。但是,我不知道该怎么做。目前我正在制作一个新列表并附加新词干,但这只是将所有列表中的所有单词放在列表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))  

例如,words=

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'>

有什么想法吗?

谢谢!

【问题讨论】:

    标签: python pandas list series


    【解决方案1】:

    将列表推导与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)])
    

    【讨论】:

      【解决方案2】:

      如果您想保留两个 for 循环,则需要使用索引来查看。在 python 中使用for i in lsti 等于列表中的每个元素。要更改列表中的值,您需要索引。

      更改循环以使用索引:

      for i in range(len(words)):
          for j in range(len(words[i])):
             words[i][j] = "something new"
      

      这将允许您更改数组中点 [i][j] 的值。

      【讨论】:

        猜你喜欢
        • 2019-11-25
        • 2018-10-24
        • 2022-09-23
        • 1970-01-01
        • 1970-01-01
        • 2023-01-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多