2016-01-10 177 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