【发布时间】:2020-06-22 20:09:08
【问题描述】:
这是我的代码:
import nltk
from nltk.corpus import stopwords
from nltk.tokenize import RegexpTokenizer
from nltk.stem import WordNetLemmatizer
import pandas as pd
import numpy as np
import openpyxl
import string
tokenizer = RegexpTokenizer(r'\w+')
lemmatizer = WordNetLemmatizer()
def remove_stopwords(df_text):
words = [w for w in df_text if w not in stopwords.words('english')]
return words
def word_lemmatizer(df_text):
lem_text = [lemmatizer.lemmatize(i) for i in df_text]
return lem_text
#works fine from here
df = pd.read_csv('amazonfresh-test.csv', encoding='utf-8', converters={'text': str})
df['text'].apply(lambda x: tokenizer.tokenize(x.lower()))
df['text'].apply(lambda x: remove_stopwords(x))
df['text'].apply(lambda x: word_lemmatizer(x))
#to here
#this is where I have issues
data_count = df['test'].apply(pd.value_counts())
data_count.to_excel("amazonfresh-test.xlsx")
它需要很长时间才能运行,我只是尝试剥离和拆分文本列每一行中的字符串,然后让总字数显示字频。
Here is what the csv looks like:
Here is the new csv with words in their own cell, still struggling to get the value_count on this.
【问题讨论】:
-
你能提供
'amazonfresh-test.csv'的样本吗? -
是的,刚刚@Phillyclause89
-
@Phillyclause89 它发生在没有错字的情况下。我能够在没有太多问题的情况下进行词形还原、删除停用词和标记化。但我有一个问题只是试图获取列中文本的值计数
-
@Phillyclause89 是的,只是想计算剩余的令牌,你建议的脚本只输出数字
-
在玩了这个之后有两个想法。 1) 请记住
pandas.Series.apply默认不是就地方法。我认为您想在这三行中执行df['text'] = df['text'].apply(func),看起来您想要对文本列中的数据进行标记和过滤。 2)我认为您想从data_count = df['test'].apply(pd.value_counts())行中的pd.value_counts函数中删除调用者。试试data_count = df['test'].apply(pd.value_counts)?
标签: python pandas dataframe series