【发布时间】:2018-06-11 10:10:34
【问题描述】:
如何将我的代码重写为可以再次调用的函数
我的代码
stopwords=nltk.corpus.stopwords.words('english')
user_defined_stop_words=['st','rd','kwun tong','kwai chung','kwun','tong']
new_stop_words=stopwords+user_defined_stop_words
data['Clean_addr'] = data['Adj_Addr'].apply(lambda x: ' '.join([item.lower() for item in x.split()]))
data['Clean_addr']=data['Clean_addr'].apply(lambda x:"".join([item.lower() for item in x if not item.isdigit()]))
data['Clean_addr']=data['Clean_addr'].apply(lambda x:"".join([item.lower() for item in x if item not in string.punctuation]))
data['Clean_addr'] = data['Clean_addr'].apply(lambda x: ' '.join([item.lower() for item in x.split() if item not in (new_stop_words)]))
cv = CountVectorizer( max_features = 200,analyzer='word',ngram_range=(1, 3))
cv_addr = cv.fit_transform(data.pop('Clean_addr'))
for i, col in enumerate(cv.get_feature_names()):
data[col] = pd.SparseSeries(cv_addr[:, i].toarray().ravel(), fill_value=0)
任何帮助表示赞赏。
【问题讨论】:
-
这不是代码重写/重构服务。您最好解释一下您要做什么,在您的问题中粘贴一些示例输入数据作为文本,以及一些预期的输出。阅读此链接:stackoverflow.com/questions/20109391/…
标签: python function pandas nltk