How to calculate word frequency of a string phrase in a column of a csv file using python?
Question: So my problem is that I have a csv file is structured something like this:

    
    
    "L.Name", "F. Name", "Gender", "School Type", "Subjects"
    "Doe",    "John",    "M",      "University",  "Chem I, statistics, English, Anatomy"
    "Tan",    "Betty",   "F",      "High School", "Algebra I, chem I, English 101"
    "Han",    "Anna",    "F",      "University",  "PHY 3, Calc 2, anatomy I, spanish 101"
    "Hawk",   "Alan",    "M",      "University",  "English 101, chem I," 
    

What I need to do is count the amount of times a subject is being taken. For
example, I need to be able to check that "Spanish 101"/"spanish 101" is being
taken 1 time, and that "Chem I"/"chem I" is being taken 3 times. However, I'm
running into a lot of problems while trying to doing it in python. I'm a
beginning programmer and just started programming in python this week, so I'm
not really sure what's the best way to approach this problem. I first tried to
do this:

    
    
    import csv 
    count = 0
    stringToCheck = "Enter Subject here"
    with open("Students.csv") as f:
        f_csv = csv.reader(f)
        headers = next(f_csv)
        for col in f_csv:
            if(col[4]):
                if stringToCheck in col[4]:
                    print('Found', stringToCheck, 'in' col[4])
                    count += 1 
                else: print('Not found in', col[4])
    print(count) 
    

However, that does not work.... for example, the first line is something like
"Concurrent programming, blah, blah blah", where the phrase "concurrent
programming" only shows up in that one line, once. When I search for either
"Concurrent programming" or "concurrent programming" i get that it returns
"Not Found in..." from rows 10 through the end.

Furthermore, I tried this:

    
    
    import csv
    import re 
    from collections import Counter 
    with open("Students.csv") as f:
        f_csv = csv.reader(f)
        for col in f_csv
            words = re.findall(r'\w+', col[4])
            two_words = [''.join(ws) for ws in zip(words,words[1:])]
            wordscount = {w:f for w, s in Counter(two_words).most_common() if s > 1}
     print (wordscount) 
    

but that was majorly wrong.... and I understand, that it doesn't do the
purpose of searching and counting for a specific phrase, but I don't know how
to turn it into that either. I sort of just looked that up online, and I
didn't completely understand how to use it anyway....

But yeah, I've been working on this for hours and its due today, any help
please??? I'm just a bit confused, and knowing what would be the right
direction would be helpful, thanks.


Answer: So there are a few weird things in your csv file (like an extra comma in the
subjects field of the last row), but I think the following should work for
you.

    
    
    import csv
    with open('Students.csv','r') as csvfile:
        reader = csv.DictReader(csvfile, delimiter=',', skipinitialspace=True)
        subjectcount={}
        for row in reader:
            for subject in row['Subjects'].lower().split(','):
                if subject.strip() not in subjectcount:
                    subjectcount[subject.strip()] = 1
                else:
                    subjectcount[subject.strip()] += 1
        for k,v in subjectcount.items():
            print k, v
    

