私が収集したメッセージの列を持つ.csvファイルがあります。その列のすべての単語の単語頻度リストを取得したいと考えています。ここに私がこれまで持っているものがあります。私はどこで間違いを犯したのか分からず、助けていただければ幸いです。編集:予想される出力は、単語のリスト全体とその数(重複なし)を別の.csvファイルに書き出すことです。PythonのCSV列からの単語頻度
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])
あなたの出力は次のようになりどういう?また、単語の頻度を得るために何かをしているようには見えません。あなたはforループと.count()を使ってみましたか? – SAMO
エラーメッセージが表示されますか?期待される結果は何ですか? [MCVE](http://stackoverflow.com/help/mcve)をご覧ください。 –
私は理想的には、2つの列を持つoutput.csvファイル(重複なし)とその単語が現れる回数の1つの列を持つことが理想的です。 – TechPadawan24