繁体   English   中英

使用Pandas DataFrame中的函数从列表创建列表

[英]Create list from list with function in pandas dataframe

我想通过在另一列中的单词列表上运行词干功能来创建新的pandas列。 我可以使用apply和lambda来标记单个字符串,但是我无法弄清楚如何将其推断为在单词列表上运行的情况。

test = {'Statement' : ['congratulations on the future','call the mechanic','more text'], 'Other' : [2,3,4]}
df = pd.DataFrame(test)
df['tokenized'] = df.apply (lambda row: nltk.word_tokenize(row['Statement']), axis=1)

我知道我可以使用嵌套的for循环来解决它,但这似乎效率低下并导致SettingWithCopyWarning:

df['stems'] = ''
for x in range(len(df)):
    print(len(df['tokenized'][x]))
    df['stems'][x] = row_stems=[]
    for y in range(len(df['tokenized'][x])):
        print(df['tokenized'][x][y])
        row_stems.append(stemmer.stem(df['tokenized'][x][y]))

有没有更好的方法可以做到这一点?

编辑:

这是结果示例的示例:

    Other     Statement                       tokenized                             stems 
0   2         congratulations on the future   [congratulations, on, the, future]    [congratul, on, the, futur]
1   3         call the mechanic               [call, the, mechanic]                 [call, the, mechan]
2   4         more text                       [more, text]                          [more, text]

实际上,无需运行循环。 至少不是显式循环。 列表理解会很好用。

假设您使用Porter stemmer ps

df['stems'] = df['tokenized'].apply(lambda words: 
                                    [ps.stem(word) for word in words])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM