【发布时间】:2019-08-15 04:59:55
【问题描述】:
我已经定义了一个列表,它读取多个文件的内容并存储所有这些文件。 如何创建一个数据框,每个文件名在一行中,相应的列计算每个单词的出现次数并输出。
为了举例,假设这一切都是定义好的(但如果需要我可以提供原始代码):
#define list
words = [ file1_contents, file2_contents ]
file1_contents = "string with dogs, cats and my pet sea turtle that lives in my box with my other turtles."
file2_contents = "another string about my squirrel, box turtle (who lives in the sea), but not my cat or dog".
filter_words = ["cat", "dog", "box turtle", "sea horse"]
输出会是这样的:
output = {'file1'{'cat': 1, 'dog':1, 'box turtle': 1, 'sea horse': 0}, 'file2'{ ...}}
我附上了我的最终目标的图片。我刚开始使用python,所以我不太确定我会在这里使用什么包/模块?我知道 pandas 可以让你使用数据框。
我有使用collections 中的Counter 的想法
from collections import Counter
z = ['blue', 'red', 'blue', 'yellow', 'blue', 'red']
Counter(z)
Counter({'blue': 3, 'red': 2, 'yellow': 1})
但是,这就是我卡住的地方。如何在 python 中组织一个看起来像所附图像的表格?
【问题讨论】:
标签: python-3.x pandas nltk counter