Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
197 views
in Technique[技术] by (71.8m points)

python - Print Nested Dictionary in Table Format

I am trying to print my nested dictionary in a table format. But have not been able to do so. The table is a list of student names and student grades. I would like it to be in a "Report Card" type format that is easy to read. The for loop I am using prints out the results. But it is one column list form.

This is my current dictionary I have setup.

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

    },
}

This is the for loop I am currently using.

for i in student:
    for j in student[i]:
        print(j + " : " + str(student[i][j]))
    print("===========")

I would like to print out in a table format.

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

I have tried to use the format() function but could not make it work with the nested dictionary.

I also tried using pandas and converting to a pd.DataFrame, but could not make it work.

question from:https://stackoverflow.com/questions/65546197/print-nested-dictionary-in-table-format

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here is a pandas solution, as you mentioned unsuccessfully trying pandas. You can use pandas.DataFrame.from_dict with orient=index.

import pandas as pd

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

You get

    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

For a markdown table

print(df.to_markdown(index=False))

| Name   |   Grade |   Math |   English |   Physics |   Chemistry |
|:-------|--------:|-------:|----------:|----------:|------------:|
| Eddy   |       1 |     78 |        65 |        89 |          80 |
| Jim    |       2 |     89 |        65 |        87 |          76 |
| Jane   |       3 |     87 |        97 |        75 |          64 |
  • The benefit of pandas over other implementations, is the data is now in a format for easy analysis and plotting.
import matplotlib.pyplot as plt

df.plot.barh(x='Name', y=['Math', 'English', 'Physics', 'Chemistry'], figsize=(6, 8))
plt.xlabel('Score')
plt.legend(title='Subject', bbox_to_anchor=(1.05, 1), loc='upper left')

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...