本文介绍了在Python中计算来自列的单词的频率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个csv文件。 csv文件的结构是:
I have a csv file. The structure of the csv file is:
Name Hour Location A 4 San Fransisco B 2 New York C 4 New York D 7 Denton E 8 Boston F 1 Boston如果您观察上述数据,则会出现
If you observe the data above, There are
2 New York and 2 Boston我试图使用表格包。我尝试了表格包文档中提到的教程超过7小时。
I tried to use the tabular package. I tried the tutorials mentioned in the tabular package documentation since more than 7 hours. But I dint get through.
任何人都可以帮助我,如何使用Python提取位置列中Csv文件中常用字的计数。
Can anyone help me, how can I extract the count of the frequent words in that Csv file in the Location column using Python.
谢谢。
推荐答案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更多推荐
在Python中计算来自列的单词的频率
发布评论