2016-01-10 122 views
1

我正在分析包含用户的评论评论的大数据文件,并且我被要求将每行转换为词典作为关键字(词)和值(该行中的词的计数/评论评论),分析单词的用法。将CSV行转换为字典

使用下面的代码,我能够拆分数据,但无法将其转换为字典。

import csv 
import pandas as pd 

products = pd.read_csv('product_comments.csv') 
products['words_count'] = csv.DictReader(products['review'].str.lower().str.split()) 

请帮我解决这个问题。

+0

向我们显示您从csv文件中读取的数据。 – vrs

+0

并正确编辑您的代码请 –

+1

'csv.DictReader'用于操作文本文件。不是熊猫的数据结构。 –

回答

0

您可以将applyCounter改为reviews列以获得词频的dictionary

基于 unix单词列表上的插图

随机抽样:对空间

word_file = "/usr/share/dict/words" 
words = open(word_file).read().splitlines()[10:50] 
random_word_list = [[' '.join(np.random.choice(words, size=100, replace=True))] for i in range(50)] 

df.head() 

              reviews 
0 abaculus abacinate abalienate abaff abalone ab... 
1 abalienation abacus abaction abacination abaca... 
2 Ababdeh abalienate abaiser abaff abaca abactin... 
3 abaction Aaru abandonee abalienate Aaronic aba... 
4 abandon abampere abactor abactor abandon abacu... 

拆分并使用DataFrame.apply()与内置collections.Counter

from collections import Counter 
df.reviews.str.split(' ').apply(lambda x: Counter(x)) 

你得到:

0  {'Ababua': 5, 'abandon': 7, 'abaction': 3, 'ab... 
1  {'Aaronical': 3, 'abandon': 1, 'abaction': 4, ... 
2  {'Aaronical': 5, 'Ababua': 1, 'abaction': 1, '... 
3  {'Aaronical': 3, 'abandon': 1, 'abaction': 7, ... 
4  {'Aaronical': 4, 'abandon': 2, 'abaction': 2, ... 
+0

这个工作,为你毕竟呢? – Stefan