Skip to content

Python print nested dictionary as table | Example code

  • by

Using the pandas module you can print a nested dictionary as a table in Python. Or you can use the tabulate library, which is not part of the Python standard library, so you’ll need to install it first using pip.

Example print nested dictionary as a table in Python

Simple example code. The df.fillna(0) replaces missing values with 0.

import pandas as pd

data = {'bin1': {'A': 14545,
                 'B': 18579,
                 'C': 5880,
                 'D': 20771,
                 'E': 404396},
        'bin2': {'A': 13200,
                 'D': 16766,
                 'E': 200344},
        }

df = pd.DataFrame(data).T
df.fillna(0, inplace=True)
print(df)

Output:

Python print nested dictionary as table

Another example

You can use pandas.DataFrame.from_dict with orient=index.

import pandas as pd

student = {
    "student1": {
        "Name": "Eddy",
        "Grade": 1,
        "Math": 78,
        "English": 65,
        "Physics": 89,
        "Chemistry": 80
    },
    "student2": {
        "Name": "Jim",
        "Grade": 2,
        "Math": 89,
        "English": 65,
        "Physics": 87,
        "Chemistry": 76

    },
    "student3": {
        "Name": "Jane",
        "Grade": 3,
        "Math": 87,
        "English": 97,
        "Physics": 75,
        "Chemistry": 64

    },
}

df = pd.DataFrame.from_dict(student, orient='index').reset_index(drop=True)

print(df)

Output:

   Name  Grade  Math  English  Physics  Chemistry
0  Eddy      1    78       65       89         80
1   Jim      2    89       65       87         76
2  Jane      3    87       97       75         64

Source: stackoverflow.com

For a markdown table

Use pandas.DataFrame.to_markdown()

print(df.to_markdown(index=False))

Output:

| Name   |   Grade |   Math |   English |   Physics |   Chemistry |
|:-------|--------:|-------:|----------:|----------:|------------:|
| Eddy   |       1 |     78 |        65 |        89 |          80 |
| Jim    |       2 |     89 |        65 |        87 |          76 |
| Jane   |       3 |     87 |        97 |        75 |          64 |

Use the following Python code to print a nested dictionary as a table:

from tabulate import tabulate

# Sample nested dictionary
data = {
    'Alice': {'Age': 25, 'City': 'New York'},
    'Bob': {'Age': 30, 'City': 'San Francisco'},
    'Charlie': {'Age': 22, 'City': 'Los Angeles'}
}

# Convert the nested dictionary to a list of dictionaries
table_data = [{key: value for key, value in entry.items()} for entry in data.values()]

# Get the table headers from the first dictionary
headers = table_data[0].keys()

# Print the table using tabulate
print(tabulate(table_data, headers, tablefmt="grid"))

Do comment if you have any doubts or suggestions on this Python print dictionary topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading