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

编程入门 行业动态 更新时间:2024-10-25 16:18:22
本文介绍了在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中计算来自列的单词的频率

本文发布于:2023-10-18 12:44:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1504266.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单词   频率   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!