基于嵌套字典创建表

时间:2017-06-29 13:00:06

标签: python dictionary format tabular

我从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中以这种格式打印出这些信息?或者,如果我使用模块,我应该使用哪个模块以及我应该使用该模块中的哪些功能?

2 个答案:

答案 0 :(得分:1)

您可以在列表中创建多个pandas DataFrame:

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

但是,您可能需要将元组更改为dict,以便pandas可以生成正确的索引。

答案 1 :(得分: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              |         |                 |               |              |                         |

修改

 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          |                         |

这就是你想要的吗?