2017-02-14 100 views
1

我有一個數據框,列表中的字符串作爲列,並希望使用collections.counter創建詞語頻率詞典。數據幀如下所示:從數據框列表創建詞語頻率詞典

>>> job_title['title'] 
0   [responsible, caring, trustworthy, babysitter] 
1    [compassionate, trustworthy, babysitter] 
2  [family, looking, kindergarten, preschool, chi... 
3  [babysitter, needed, 2, children, bee, cave, n... 
4    [fun, patient, nonjudgemental, babysitter] 
5  [responsible, interactive, intelligent, babysi... 
6     [responsible, friendly, babysitter] 
7  [family, looking, kindergarten, preschool, chi... 
8  [family, looking, kindergarten, preschool, chi... 
9      [reliable, clean, friendly, nanny] 

什麼是最有效的方法來實現這一目標?

回答

1

我想你可以扁平lists通過chain.from_iterable然後用Counter

from itertools import chain 
from collections import Counter 

print (Counter(chain.from_iterable(job_title.title))) 

樣品:

job_title = pd.DataFrame({'title':[['responsible', 'caring', 'trustworthy', 'babysitter'], 
            ['compassionate', 'trustworthy', 'babysitter']]}) 

print (job_title) 
              title 
0 [responsible, caring, trustworthy, babysitter] 
1  [compassionate, trustworthy, babysitter] 


print (Counter(chain.from_iterable(job_title.title))) 
Counter({'babysitter': 2, 'trustworthy': 2, 
     'compassionate': 1, 'responsible': 1, 'caring': 1}) 
+0

這是完美的,太感謝你了! – mangodreamz