繁体   English   中英

基于嵌套字典创建表

[英]Creating a table based on nested dictionary

我从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')]}}

信息需要以这种方式格式化:

在此处输入图片说明

如何在python中以这种格式打印此信息? 或者,如果我使用模块,则应使用哪个模块以及该模块中应使用的功能?

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

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

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

我想向您建议:

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

那是你要的吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM