python - 如何将嵌套字典呈现为表格

标签 python python-3.x dictionary printing format

我有以下字典:

Dictionary

我想打印出这些词典,以便它们出现在这样一个易于阅读的表格中,无需使用 pandas:

Output should look like this

我目前拥有的:

Current code

我能够打印表格标题,但在映射其他列时遇到问题。

这是要复制的文本字典:

{'ffa': {'cope1': 0.6525,
         'cope2': 0.4146,
         'cope3': 0.5896,
         'cope4': 0.1521,
         'cope5': 0.5317},
 'lingual': {'cope1': -0.08865060000000001,
             'cope2': -0.150985,
             'cope3': -0.162005,
             'cope4': -0.130845,
             'cope5': -0.126411},
 'ppa': {'cope1': 0.74836,
         'cope2': 0.9444,
         'cope3': 0.300482,
         'cope4': 1.12435,
         'cope5': 0.8332200000000001}}

最佳答案

选项 1 - Pandas

我建议使用 pandas为此的数据框:

import pandas as pd
d = {'ffa': {'cope1': 0.6525, 'cope2': 0.4146, 'cope3': 0.5896, 'cope4': 0.1521, 'cope5': 0.5317}, 'lingual': {'cope1': -0.08865060000000001, 'cope2': -0.150985, 'cope3': -0.162005, 'cope4': -0.130845, 'cope5': -0.126411}, 'ppa': {'cope1': 0.74836, 'cope2': 0.9444, 'cope3': 0.300482, 'cope4': 1.12435, 'cope5': 0.8332200000000001}}
data = [(k,*x)  for k, v in d.items() for x in v.items()]
df = pd.DataFrame(data, columns = ['ROI', 'COPE', 'MEAN'])

给予:

>>> df
        ROI   COPE      MEAN
0       ffa  cope1  0.652500
1       ffa  cope2  0.414600
2       ffa  cope3  0.589600
3       ffa  cope4  0.152100
4       ffa  cope5  0.531700
5   lingual  cope1 -0.088651
6   lingual  cope2 -0.150985
7   lingual  cope3 -0.162005
8   lingual  cope4 -0.130845
9   lingual  cope5 -0.126411
10      ppa  cope1  0.748360
11      ppa  cope2  0.944400
12      ppa  cope3  0.300482
13      ppa  cope4  1.124350
14      ppa  cope5  0.833220

选项 2 - 只打印表格

不使用 pandas 只打印:

d = {'ffa': {'cope1': 0.6525, 'cope2': 0.4146, 'cope3': 0.5896, 'cope4': 0.1521, 'cope5': 0.5317}, 'lingual': {'cope1': -0.08865060000000001, 'cope2': -0.150985, 'cope3': -0.162005, 'cope4': -0.130845, 'cope5': -0.126411}, 'ppa': {'cope1': 0.74836, 'cope2': 0.9444, 'cope3': 0.300482, 'cope4': 1.12435, 'cope5': 0.8332200000000001}}
data = [(k,*x)  for k, v in d.items() for x in v.items()]
print ("ROI            COPE        MEAN")
for l in data:
        print ("{:<14}{:<11}{}".format(l[0],l[1],l[2]))

给予:

ROI            COPE        MEAN
ffa           cope1      0.6525
ffa           cope2      0.4146
ffa           cope3      0.5896
ffa           cope4      0.1521
ffa           cope5      0.5317
lingual       cope1      -0.08865060000000001
lingual       cope2      -0.150985
lingual       cope3      -0.162005
lingual       cope4      -0.130845
lingual       cope5      -0.126411
ppa           cope1      0.74836
ppa           cope2      0.9444
ppa           cope3      0.300482
ppa           cope4      1.12435
ppa           cope5      0.8332200000000001

关于python - 如何将嵌套字典呈现为表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58997558/

相关文章:

python-3.x - 如何将数据从谷歌云加载到 jupyter notebook VM?

python-3.x - 如何将数据类型更改为 float64 以便 sklearn 可以在数据大于 np.float32 的数据帧上工作

list - 如何从 map 中获取 optional 值作为 optional 列表?

json - 如何将字典转换为不带空格和换行符的json字符串

c++ - 在 std::map 中更改元素键的最快方法是什么

python - 我们可以在 conftest.py 之外定义 pytest 钩子(Hook)吗?

python - Flask:在 View 中获取蓝图的 url_prefix

python - 使用 Pandas GroupBy 和 value_counts 查找最常见的值

python - Python 文本中重复的短语 _ Follow up

python - 基于列值 reshape Pandas 数据框