2017-06-29 20 views
0

データがどのように構造化されているかをマップする入れ子になった辞書をCSVファイルから作成しました。私は今、必ずしも表である必要はありません(表形式でデータを再配置する必要がありますが、それだけで理解できる方法で配置する必要が入れ子になった辞書に基づいてテーブルを作成する

ネストされた辞書は次のようになります。

{ 'CA': { 'Bay Area ': [ ('warm? ', 'yes\n'), 
           ('East/West Coast? ', 'West \n')], 
       'SoCal ': [ ('north or south? ', 'south \n'), 
          ('warm ', 'yes \n')]}, 
    'MA': { 'Boston ': [ ('East/West Coast? ', 'East \n'), 
          ('like it there? ', 'yes\n')], 
       'Pioneer Valley ': [ ('East/West Coast? ', 'East \n'), 
            ('city? ', 'no\n'), 
            ('college town? ', 'yes\n')]}, 
    'NY': { 'Brooklyn ': [ ('East/West Coast? ', 'East \n'), 
           ('been there? ', 'yes\n'), 
           ('Been to coney island? ', 'yes\n')], 
       'Manhattan ': [ ('East/West Coast? ', 'East \n'), 
           ('been there? ', 'yes\n')], 
       'Queens ': [ ('East/West Coast? ', 'East \n'), 
          ('been there? ', 'yes\n')], 
       'Staten Island ': [('is island? ', 'yes\n')]}} 

情報はこのようにフォーマットする必要があります

enter image description here

どのように私はPythonでこの形式でこの情報をプリントアウトするかまたは私はモジュールを使用する場合、どのようなモジュール私が使うのですか、何?そのmoduの関数私は使うべきですか?

+0

パンダまたはモジュール表を使用できます。ここをクリックしてください:https://stackoverflow.com/questions/9535954/printing-lists-as-tabular-data。とにかく、その方法を調整する必要があります。 – CunivL

答えて

1

あなたは、リスト内の複数のパンダのデータフレームを作成することができます。

import pandas as pd 
l = [] 
for subd in d: # d is your dict 
    l.append(pd.DataFrame(subd)) 

をしかし、あなたはパンダが正しいインデックスを生成することができるように、辞書にあなたのタプルを変更する必要があります。私はあなたにこの提案したい

0

import tabulate 

headers = ["City", "City2", "East/West Coast?", "north or south?", "like it there?", "city?", "college town?", "been there?", "is island?", "Been to coney island?"] 

table = [] 
for city in dictionary.keys(): 
    for city2 in dictionary[city].keys(): 
     new_row = [city] 
     new_row.append(city2) 
     for index_head in range(2, len(headers)): 
      found = False 
      for index in range(0, len(dictionary[city][city2])): 
       if headers[index_head] in dictionary[city][city2][index]: 
        found = True 
        break 
      if found: 
       new_row.append(dictionary[city][city2][index][1].replace("\n", "")) 
      else: 
       new_row.append(" ") 
     table.append(new_row) 


print(tabulate.tabulate(table, headers=headers, tablefmt="orgtbl")) 

出力は次のとおりです。

| City | City2   | East/West Coast? | north or south? | like it there? | city? | college town? | been there? | is island? | Been to coney island? | 
|--------+----------------+--------------------+-------------------+------------------+---------+-----------------+---------------+--------------+-------------------------| 
| CA  | SoCal   |     | south    |     |   |     |    |    |       | 
| CA  | Bay Area  | West    |     |     |   |     |    |    |       | 
| NY  | Staten Island |     |     |     |   |     |    | yes   |       | 
| NY  | Brooklyn  | East    |     |     |   |     | yes   |    | yes      | 
| NY  | Manhattan  | East    |     |     |   |     | yes   |    |       | 
| NY  | Queens   | East    |     |     |   |     | yes   |    |       | 
| MA  | Pioneer Valley | East    |     |     | no  | yes    |    |    |       | 
| MA  | Boston   | East    |     | yes    |   |     |    |    |       | 

EDIT

import tabulate 

headers = ["City", "East/West Coast?", "north or south?", "like it there?", "city?", "college town?", "been there?", "is island?", "Been to coney island?"] 

for city in dictionary.keys(): 
    table = [] 
    for city2 in dictionary[city].keys(): 
     new_row = [city] 
     new_row.append(city2) 
     for index_head in range(1, len(headers)): 
      found = False 
      for index in range(0, len(dictionary[city][city2])): 
       if headers[index_head] in dictionary[city][city2][index]: 
        found = True 
        break 
      if found: 
       new_row.append(dictionary[city][city2][index][1].replace("\n", "")) 
      else: 
       new_row.append(" ") 
     table.append(new_row) 
print(city) 
print(tabulate.tabulate(table, headers=headers, tablefmt="orgtbl")) 

これが出力されます。

CA 
| City  | East/West Coast? | north or south? | like it there? | city? | college town? | been there? | is island? | Been to coney island? | 
|----------+--------------------+-------------------+------------------+---------+-----------------+---------------+--------------+-------------------------| 
| SoCal |     | south    |     |   |     |    |    |       | 
| Bay Area | West    |     |     |   |     |    |    |       | 

MA 
| City   | East/West Coast? | north or south? | like it there? | city? | college town? | been there? | is island? | Been to coney island? | 
|----------------+--------------------+-------------------+------------------+---------+-----------------+---------------+--------------+-------------------------| 
| Pioneer Valley | East    |     |     | no  | yes    |    |    |       | 
| Boston   | East    |     | yes    |   |     |    |    |       | 

NY 
| City   | East/West Coast? | north or south? | like it there? | city? | college town? | been there? | is island? | Been to coney island? | 
|---------------+--------------------+-------------------+------------------+---------+-----------------+---------------+--------------+-------------------------| 
| Manhattan  | East    |     |     |   |     | yes   |    |       | 
| Queens  | East    |     |     |   |     | yes   |    |       | 
| Brooklyn  | East    |     |     |   |     | yes   |    | yes      | 
| Staten Island |     |     |     |   |     |    | yes   |       | 

それはあなたが望むものですか?

+0

私はMasterDictの最高レベルのキーのそれぞれのテーブルを必要とします。 (CA、NY、MAの元のテーブル) – question610

関連する問題