在Python中计算列中单词的频率

2024-04-29 14:58:43 发布

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

我有一个csv文件。 csv文件的结构是:

Name Hour Location
A    4    San Fransisco
B    2    New York
C    4    New York
D    7    Denton
E    8    Boston
F    1    Boston

如果你观察上面的数据,有

2 New York and
2 Boston

我试着用表格包装。从7个多小时以来,我尝试了表格包文档中提到的教程。但我没能通过。

有谁能帮我,我怎么能用Python在Location列中提取Csv文件中频繁出现的单词数呢。

谢谢你。


Tags: and文件csv数据namenewlocation结构
3条回答
data = """Name\tHour\tLocation
A\t4\tSan Fransisco
B\t2\tNew York
C\t4\tNew York
D\t7\tDenton
E\t8\tBoston
F\t1\tBoston
"""

import csv
import StringIO
from collections import Counter


input_stream = StringIO.StringIO(data)
reader = csv.reader(input_stream, delimiter='\t')

reader.next() #skip header
cities = [row[2] for row in reader]

for (k,v) in Counter(cities).iteritems():
    print "%s appears %d times" % (k, v)

输出:

San Fransisco appears 1 times
Denton appears 1 times
New York appears 2 times
Boston appears 2 times

如果文件不太大,最简单的方法是:

  • 逐行读取文件
  • 将location的值追加到列表中
  • 从列表中创建一组uniques
  • 确定列表中每个unique的计数

不确定分隔的是什么,但是示例显示为4个空格,因此这是一个解决方案。

如果你真的是用制表符分隔,请使用@MariaZverina的答案

import collections

with open('test.txt') as f:
    next(f) # Skip the first line
    print collections.Counter(line.rstrip().rpartition('    ')[-1] for line in f)

输出:

Counter({'New York': 2, 'Boston': 2, 'San Fransisco': 1, 'Denton': 1})

相关问题 更多 >