【发布时间】:2018-11-28 21:43:04
【问题描述】:
我有以下示例数据框:
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中每个词的出现频率按类别字段
示例 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
案例 2) 的以下示例所需输出:
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'])
TypeError: unhashable type: 'list'
我不知道该怎么做 2)
【问题讨论】:
-
您期望的
counts是否按category分组? -
是的,按类别分组的不同单词的计数
标签: python pandas nltk counter word