python - 在 pandas 中执行 nltk.stem.SnowballStemmer
问题描述
我有一个四列 DataFrame,其中两列标记化单词已删除停用词并转换为小写,现在正尝试进行词干化。
我不确定该apply()
方法是否访问该系列及其单个单元格,或者我是否需要另一种方式进入每条记录,所以两者都尝试了(我认为!)
from nltk.stem import SnowballStemmer
stemmer = nltk.stem.SnowballStemmer('english')
df_2['Headline'] = df_2['Headline'].apply(lambda x: stemmer.stem(item) for item in x)
-------------------------------------------------- ------------------------- TypeError Traceback (last last 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 在 pandas._libs.lib.map_infer()
TypeError:“生成器”对象不可调用
我相信这个 TypeError 类似于说 'List' 对象不可调用的那个,并且用这个apply()
方法修复了那个,并且在这里没有想法。
df_2['Headline'] = df_2['Headline'].apply(lambda x: stemmer.stem(x))
-------------------------------------------------- ------------------------- AttributeError Traceback (最近一次调用最后一次) in () ----> 1 df_2['Headline'] = df_2 ['标题'].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 在 pandas._libs.lib.map_infer()
在 (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 如果 self.stopwords 或 len(word) <= 2 中的单词:
AttributeError: 'list' 对象没有属性 'lower'
解决方案
您需要axis
为apply
.
这是一个完整的工作示例:
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 - 如何提取子文件夹路径?
- android - 使用 react-native 捕获细胞信号强度
- performance - 使用 K6(负载影响)对 API 进行负载测试时的限制
- r - 从向量中采样元素对但不重复
- javascript - 如何否定正则表达式模式
- angular - 动态创建的组件样式不起作用。ng-reflect-ng-style="[对象对象]"
- javascript - 无法使用 onclick 调用名为 clear 的函数
- mongodb - 使用正则表达式和 tx 时间范围查询输出地址
- python - Tkinter:如何将元组从列表框转换为列表?
- postgresql - 使用数组去 postgres `SELECT * IN`