您可以将apply
Counter
改为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, ...
向我们显示您从csv文件中读取的数据。 – vrs
并正确编辑您的代码请 –
'csv.DictReader'用于操作文本文件。不是熊猫的数据结构。 –