使用外部csv文件计算Python列中单词的出现频率 [英] Count the frequency of words from a column in Python using external csv file

查看:115
本文介绍了使用外部csv文件计算Python列中单词的出现频率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

user907629

This question was asked before by user907629, and Maria Zverina answered the question, but she didn't import the data from external csv file.

我的文件包含超过800000条记录,并且我想导入一个外部csv文件.在此频率计数代码?

My file contains more than 800000 records, and I want to import an external csv file. What changes should be done in this frequency count code?

推荐答案

您可以执行此操作而无需存储任何中间列表:

You can do it without storing any intermediary lists:

import csv
from collections import Counter
from itertools import imap
from operator import  itemgetter

with open('yourcsv') as f:
    next(f) # skip the header
    cn = Counter(imap(itemgetter(2), csv.reader(f)))

    for t in cn.iteritems():
        print("{} appears {} times".format(*t))

除非计划使用列表,否则没有理由将数据存储在列表中,itemgetter将仅从每一行中提取第三列值.您需要传递要计数的任何列,并将定界符设置为对数据进行定界的任何内容.

There is no reason to store data in lists unless you plan on using the list, itemgetter will pull just the third column value from each row. You need to pass whatever column you want to count and set the delimiter to whatever delimits your data.

这篇关于使用外部csv文件计算Python列中单词的出现频率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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