如何使用pandas查找文本数据中单词的出现频率并将其写入csv文件

2024-04-19 13:19:17 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我删除了停止词,对文本数据进行了符号化和计数向量化

我的代码

 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

Tags: 文件csvlambdaincleanfordataif
1条回答
网友
1楼 · 发布于 2024-04-19 13:19:17

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

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

相关问题 更多 >