Python algorithm of counting occurrence of specific word in csv

laotanzhurou picture laotanzhurou · Feb 12, 2012 · Viewed 16.9k times · Source

I've just started to learn python. I'm curious about what are the efficient ways to count the occurrence of a specific word in a CSV file, other than simply use for loop to go through line by line and read.

To be more specific, let's say I have a CSV file contain two columns, "Name" and "Grade", with millions of records.

How would one count the occurrence of "A" under "Grade"?

Python code samples would be greatly appreciated!

Answer

reclosedev picture reclosedev · Feb 12, 2012

Basic example, with using csv and collections.Counter (Python 2.7+) from standard Python libraly:

import csv
import collections

grades = collections.Counter()
with open('file.csv') as input_file:
    for row in csv.reader(input_file, delimiter=';'):
        grades[row[1]] += 1

print 'Number of A grades: %s' % grades['A']
print grades.most_common()

Output (for small dataset):

Number of A grades: 2055
[('A', 2055), ('B', 2034), ('D', 1995), ('E', 1977), ('C', 1939)]