0

I created a nested dictionary from a CSV file that maps out the way the data is structured. I now need to rearrange the data in a table format, (does not necessarily need to be in a table but it just needs to be arranged in a way that is understandable.

The nested dictionary looks like this:

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

The information needs to be formatted into this way:

enter image description here

How do I print this information out in this format in python? Or if I use a module, what module do I use and what functions in that module should I use?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
question610
  • 41
  • 1
  • 9
  • You could use Pandas or the module tabular. Give a look here: https://stackoverflow.com/questions/9535954/printing-lists-as-tabular-data. Anyway, you should adjust that method for dict. – CLeo-Dev Jun 29 '17 at 13:09

2 Answers2

0

You can create multiple pandas DataFrames in a list :

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

However, you might need to change your tuples to dict, so that pandas can generate correct indexes.

Thomas Dussaut
  • 728
  • 1
  • 7
  • 20
0

I would like to suggest you this:

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

The output is:

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

This is the output:

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

Is that what you want?

CLeo-Dev
  • 196
  • 12