2016-03-29 3 views
0

他のすべての回答リストのリスト内のすべてのネストされたリストを集計するリストごとに別々に集計します。リストのリスト内のリストごとに別々のCounter()オブジェクトとPandas DataFrameを作成する

私は現在、リストのリストを持っている:

master_list = [[a,a,b,b,b,c,c,c], [d,d,d,a,a,a,c,c,c], [c,c,c,a,a,f,f,f]] 

私は辞書やカウンターを(戻したい)がループで各リストのためのオブジェクト:

現在
counter1 = {'a': 2, 'b': 3, 'c': 3} 
counter2 = {'d': 3, 'a': 3, 'c': 3} 
counter3 = {'c': 3, 'a': 2, 'f': 3} 

、私は戻っていますループを使用してこのように見えるものです。すべてが一括して欲しいものではなく、カウンタオブジェクトに別々にアクセスする際に問題があります。

Input: 

count = Counter() 
for lists in master_list: 
    for words in lists: 
    count[words] += 1 


Output: 

Counter({'a': 2, 'b': 3, 'c': 3}) 
Counter({'d': 3, 'a': 3, 'c': 3}) 
Counter({'c': 3, 'a': 2, 'f': 3}) 

上記の問題は、これらのディクショナリ/カウンタオブジェクトのそれぞれについてパンダのデータフレームを作成しようとしているため、各カウンタを個別に取得する方法がわからないことです。 "master_list"には何百ものリストが含まれていて、それぞれのリストの要素の頻度を示すデータフレームを返したいので、プログラムでこれを実行しようとしています。 - また、

Input: 

table = pandas.DataFrame(count.items()) 
table.columns = ['Word', 'Frequency'] 
table.sort_values(by=['Frequency'], ascending = [False]) 


Output: 

Word Frequency 
the 542 
and 125 
or  45 
.  . 
.  . 
.  . 
.  . 

任意の洞察力をいただければ幸いです。最後に、私は現在、私が唯一の1データフレームを返す何かを持っている「マスターリスト」内

のすべてのリストについては、個別のデータフレームとカウンターオブジェクトを持っているでしょうCounter()オブジェクトを別々に処理する上でのヒントは、別々に評価されます。

+0

まさにあなたが参照していますか? – mk8efz

答えて

0

リストを作成し、カウンタを追加することができます。 (不要である、また、あなたはCounterを使用しているが、それでもカウントを自分でやって。)

master_list = [[a,a,b,b,b,c,c,c], [d,d,d,a,a,a,c,c,c], [c,c,c,a,a,f,f,f]] 
counters = [] 
for list_ in master_list: 
    counters.append(Counter(list_)) 

を今、あなたはcounters[i]でそれぞれ別のリストに対処することができます。

+0

これはうまくいきました、ありがとうございます。私は、辞書のリストが完全に細かいデータ構造であることに気付かなかった。 – mk8efz

0

IMO、この質問は本物のパンダの力を示すことができます。以下のようにしましょう。退屈な数をカウントする代わりに、[a,a,b,b,b,c,c,c], [d,d,d,a,a,a,c,c,c], [c,c,c,a,a,f,f,f]本の本の単語の頻度を数えます。私は「ファウスト」、「ハムレット」、「マクベス」の3つを選んだ。

コード:

#!/usr/bin/env python 
# -*- coding: utf-8 -*- 

from collections import defaultdict 
import string 
import requests 
import pandas as pd 

books = { 
    'Faust': 'http://www.gutenberg.org/cache/epub/2229/pg2229.txt', 
    'Hamlet': 'http://www.gutenberg.org/cache/epub/2265/pg2265.txt', 
    'Macbeth': 'http://www.gutenberg.org/cache/epub/2264/pg2264.txt', 
} 

# prepare translate table, which will remove all punctuations and digits 
chars2remove = list(string.punctuation + string.digits) 
transl_tab = str.maketrans(dict(zip(chars2remove, list(' ' * len(chars2remove))))) 
# replace 'carriage return' and 'new line' characters with spaces 
transl_tab[10] = ' ' 
transl_tab[13] = ' ' 


def tokenize(s): 
    return s.translate(transl_tab).lower().split() 

def get_data(url): 
    r = requests.get(url) 
    if r.status_code == requests.codes.ok: 
     return r.text 
    else: 
     r.raise_for_status() 

# generate DF containing words from books 
d = defaultdict(list) 
for name, url in books.items(): 
    d[name] = tokenize(get_data(url)) 

df = pd.concat([pd.DataFrame({'book': name, 'word': tokenize(get_data(url))}) 
       for name, url in books.items()], ignore_index=True) 

# let's count the frequency 
frequency = df.groupby(['book','word']) \ 
       .size() \ 
       .sort_values(ascending=False) 

# output 
print(frequency.head(30)) 
print('[Macbeth]: macbeth\t', frequency.loc['Macbeth', 'macbeth']) 
print('[Hamlet]: nay\t', frequency.loc['Hamlet', 'nay']) 
print('[Faust]: faust\t', frequency.loc['Faust', 'faust']) 

出力:

book  word 
Hamlet the  1105 
     and  919 
Faust und  918 
Hamlet to  760 
Macbeth the  759 
Hamlet of  698 
Faust ich  691 
     die  668 
     der  610 
Macbeth and  602 
Hamlet you  588 
     i   560 
     a   542 
     my  506 
Macbeth to  460 
Hamlet it  439 
Macbeth of  426 
Faust nicht  426 
Hamlet in  409 
Faust das  403 
     ein  399 
     zu  380 
Hamlet that  379 
Faust in  365 
     ist  363 
Hamlet is  346 
Macbeth i   344 
Hamlet ham  337 
     this  328 
     not  316 
dtype: int64 

[Macbeth]: macbeth  67 
[Hamlet]: nay 27 
[Faust]: faust 272 
関連する問題