I have the following sample dataframe:
No category problem_definition
175 2521 ['coffee', 'maker', 'brewing', 'properly', '2', '420', '420', '420']
211 1438 ['galley', 'work', 'table', 'stuck']
912 2698 ['cloth', 'stuck']
572 2521 ['stuck', 'coffee']
problem_definition字段已使用停用词进行分词处理。
我想创建一个频率分布,输出另一个Pandas数据框:
1)包含problem_definition中每个单词的出现频率 2)包含problem_definition和category字段中每个单词的出现频率
示例所需输出如下(针对情况1):
text count
coffee 2
maker 1
brewing 1
properly 1
2 1
420 3
stuck 3
galley 1
work 1
table 1
cloth 1
以下是第二种情况所需的样本输出:
category text count
2521 coffee 2
2521 maker 1
2521 brewing 1
2521 properly 1
2521 2 1
2521 420 3
2521 stuck 1
1438 galley 1
1438 work 1
1438 table 1
1438 stuck 1
2698 cloth 1
2698 stuck 1
我尝试了下面的代码来实现1):
from nltk.probability import FreqDist
import pandas as pd
fdist = FreqDist(df['problem_definition_stopwords'])
类型错误:不可散列类型:'list'
我不知道如何完成第二个任务
counts
是否按category
分组? - BernardL