gpt4 book ai didi

python - 在 pandas 中执行 nltk.stem.SnowballStemmer

转载 作者:行者123 更新时间:2023-12-01 06:34:56 25 4
gpt4 key购买 nike

我有一个四列 DataFrame,其中有两列标记化单词,这些单词已删除停用词并转换为小写,现在正在尝试阻止。

enter image description here

我不确定 apply() 方法是否访问该系列及其各个单元格,或者我是否需要另一种方式进入每个记录,因此尝试了这两种方法(我认为!)

从 nltk.stem 导入 SnowballStemmer
stemmer = nltk.stem.SnowballStemmer('english')

我试过了:

df_2['Headline'] = df_2['Headline'].apply(lambda x: Stemmer.stem(item) for x 中的项目)

--------------------------------------------------------------------------- TypeError Traceback (most recent call last) in () ----> 1 df_2['Headline__'] = df_2['Headline'].apply(lambda x: stemmer.stem(item) for item in x)

~\AppData\Local\Continuum\anaconda3\envs\learn-env\lib\site-packages\pandas\core\series.py in apply(self, func, convert_dtype, args, **kwds) 3192
else: 3193 values = self.astype(object).values -> 3194 mapped = lib.map_infer(values, f, convert=convert_dtype) 3195 3196 if len(mapped) and isinstance(mapped[0], Series):

pandas/_libs/src\inference.pyx in pandas._libs.lib.map_infer()

TypeError: 'generator' object is not callable

我相信这个 TypeError 类似于“List”对象不可调用的错误,并使用 apply() 方法修复了该错误,但这里没有想法。

df_2['Headline'] = df_2['Headline'].apply(lambda x: Stemmer.stem(x))

--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) in () ----> 1 df_2['Headline'] = df_2['Headline'].apply(lambda x: stemmer.stem(x)) 2 3 df_2.head()

~\AppData\Local\Continuum\anaconda3\envs\learn-env\lib\site-packages\pandas\core\series.py in apply(self, func, convert_dtype, args, **kwds) 3192
else: 3193 values = self.astype(object).values -> 3194 mapped = lib.map_infer(values, f, convert=convert_dtype) 3195 3196 if len(mapped) and isinstance(mapped[0], Series):

pandas/_libs/src\inference.pyx in pandas._libs.lib.map_infer()

in (x) ----> 1 df_2['Headline'] = df_2['Headline'].apply(lambda x: stemmer.stem(x)) 2 3 df_2.head()

~\AppData\Local\Continuum\anaconda3\envs\learn-env\lib\site-packages\nltk\stem\snowball.py in stem(self, word) 1415 1416 """ -> 1417 word = word.lower() 1418 1419 if word in self.stopwords or len(word) <= 2:

AttributeError: 'list' object has no attribute 'lower'

最佳答案

您需要为apply指定axis

这是一个完整的工作示例:

import pandas as pd

df = pd.DataFrame({
'col_1' : [['ducks'], ['dogs']],
'col_2' : [['he', 'eats', 'apples'], ['she', 'has', 'cats', 'dogs']],
'col_3' : ['some data 1', 'some data 2'],
'col_4' : ['another data 1', 'another data 2']
})
df.head()

输出

    col_1   col_2                   col_3       col_4
0 [ducks] [he, eats, apples] some data 1 another data 1
1 [dogs] [she, has, cats, dogs] some data 2 another data 2

现在让我们对标记化列应用词干分析:

import nltk
from nltk.stem import SnowballStemmer
stemmer = nltk.stem.SnowballStemmer('english')

df.col_1 = df.apply(lambda row: [stemmer.stem(item) for item in row.col_1], axis=1)
df.col_2 = df.apply(lambda row: [stemmer.stem(item) for item in row.col_2], axis=1)

检查数据帧的新内容。

df.head()

输出

    col_1   col_2                   col_3       col_4
0 [duck] [he, eat, appl] some data 1 another data 1
1 [dog] [she, has, cat, dog] some data 2 another data 2

关于python - 在 pandas 中执行 nltk.stem.SnowballStemmer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59719477/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com