如何使用pandas在文本数据中查找和写入单词出现的频率到csv文件中

时间:2017-12-11 05:57:24

标签: python pandas scikit-learn nlp

如何在python中创建包含单词及其出现频率的csv文件。

我删除了停用词,tokenized和countvectorized文本数据

我的代码

 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')
        cv_addr = cv.fit_transform(data.pop('Clean_addr'))

我正在使用的文件的样本转储

https://www.dropbox.com/s/allhfdxni0kfyn6/Test.csv?dl=0

**Expected output**
Word       Freq
Industry    40
Limited     23
House       45
flat        56

1 个答案:

答案 0 :(得分:1)

您可以先创建DataFrame然后再创建sum

df1 = pd.DataFrame(cv_addr.todense(), columns=cv.get_feature_names())
df1 = df1.sum().rename_axis('Word').reset_index(name='Freq')