title: Word Frequency from a CSV Column in Python
body: <p>I have a .csv file with a column of messages I have collected, I wish to get a word frequency list of every word in that column. Here is what I have so far and I am not sure where I have made a mistake, any help would be appreciated. Edit: The expected output is to write the entire list of words and their count (without duplicates) out to another .csv file.</p>
  
  <pre><code>import csv
  from collections import Counter
  from collections import defaultdict
  
  output_file = 'comments_word_freqency.csv'
  input_stream = open('comments.csv')
  reader = csv.reader(input_stream, delimiter=',')
  reader.next() #skip header
  csvrow = [row[3] for row in reader] #Get the fourth column only
  
  with open(output_file, 'rb') as csvfile:
      for row in reader:
          freq_dict = defaultdict(int) # the "int" part
                                      # means that the VALUES of the dictionary are integers.
          for line in csvrow:
              words = line.split(" ")
              for word in words:
                  word = word.lower() # ignores case type
                  freq_dict[word] += 1
  
          writer = csv.writer(open(output_file, "wb+")) # this is what lets you write the csv file.
          for key, value in freq_dict.items():
                          # this iterates through your dictionary and writes each pair as its own line.
              writer.writerow([key, value])
  </code></pre>