我有一个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文件中频繁出现的单词数呢。
谢谢您。

最佳答案

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

10-06 12:57