在Python中计算来自列的单词的频率 [英] Count the frequency of words from a column in Python

查看:172
本文介绍了在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中计算来自列的单词的频率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆