python - 如何将列表中的每个字典转换为Python中的嵌套字典?

标签 python pandas numpy dictionary pearson-correlation

我正在使用 pandasnumpy 库来计算两个简单列表的pearson 相关性。以下代码的输出是相关矩阵:

import numpy as np
import pandas as pd

x = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
y = np.array([2, 1, 4, 5, 8, 12, 18, 25, 96, 48])
z = np.array([5, 3, 2, 1, 0, -2, -8, -11, -15, -16])

x, y, z = pd.Series(x), pd.Series(y), pd.Series(z)

xyz = pd.DataFrame({'dist-values': x, 'uptime-values': y, 'speed-values': z})


matrix = xyz.corr(method="pearson")

在输出上使用 .unstack().to_dict() 函数后,我们可以拥有以下格式的字典,并基于以下答案这个post ,我们可以将输出转换为字典列表:

result = (matrix.unstack().rename_axis(['f1', 'f2'])
                          .reset_index(name='value').to_dict('records')
         )
# the output format after printing
[{'f1': 'dist-values', 'f2': 'dist-values', 'value': 1.0}, 
 {'f1': 'dist-values', 'f2': 'uptime-values', 'value': 0.7586402890911869}, 
 {'f1': 'dist-values', 'f2': 'speed-values', 'value': -0.9680724198337364}, 

 {'f1': 'uptime-values', 'f2': 'dist-values', 'value': 0.7586402890911869}, 
 {'f1': 'uptime-values', 'f2': 'uptime-values', 'value': 1.0}, 
 {'f1': 'uptime-values', 'f2': 'speed-values', 'value': -0.8340792243486527}, 

 {'f1': 'speed-values', 'f2': 'dist-values', 'value': -0.9680724198337364}, 
 {'f1': 'speed-values', 'f2': 'uptime-values', 'value': -0.8340792243486527}, 
 {'f1': 'speed-values', 'f2': 'speed-values', 'value': 1.0}]

但是我需要更复杂的格式,输出应该是这样的:

[ 
 {'name': 'dist-values', 'data': [{'x': 'dist-values', 'y': 1.0}, {'x': 'uptime-values', 'y': 0.7586402890911869}, {'x': 'speed-values', 'y': -0.9680724198337364}]}, 
 {'name': 'uptime-values', 'data': [{'x': 'dist-values', 'y': 0.7586402890911869}, {'x': 'uptime-values', 'y': 1.0}, {'x': 'speed-values', 'y': -0.8340792243486527}]}, 
 {'name': 'speed-values', 'data': [{'x': 'dist-values', 'y': -0.9680724198337364}, {'x': 'uptime-values', 'y': -0.8340792243486527}, {'x': 'speed-values', 'y': 1.0}]}, 
]

这段代码中只有三个特征,相关矩阵也只有9个元素,但是在更大的矩阵中,我们如何实现这种转换呢?有没有一种有效的方法来做到这一点?谢谢。

最佳答案

您可以尝试列表理解来获取输出:

out = [
    {"name": i, "data": [{"x": c, "y": row[c]} for c in row.index]}
    for i, row in matrix.iterrows()
]
print(out)

打印:

[
    {
        "name": "dist-values",
        "data": [
            {"x": "dist-values", "y": 1.0},
            {"x": "uptime-values", "y": 0.7586402890911869},
            {"x": "speed-values", "y": -0.9680724198337364},
        ],
    },
    {
        "name": "uptime-values",
        "data": [
            {"x": "dist-values", "y": 0.7586402890911869},
            {"x": "uptime-values", "y": 1.0},
            {"x": "speed-values", "y": -0.8340792243486527},
        ],
    },
    {
        "name": "speed-values",
        "data": [
            {"x": "dist-values", "y": -0.9680724198337364},
            {"x": "uptime-values", "y": -0.8340792243486527},
            {"x": "speed-values", "y": 1.0},
        ],
    },
]

关于python - 如何将列表中的每个字典转换为Python中的嵌套字典?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73178355/

相关文章:

python - * 之后未通过 render() 参数传递的参数必须是可迭代的,而不是 pygame.Surface

pandas - 如何在 polars DataFrame 中的每一列与该列的平均值之间轻松执行计算

python - 如何根据列值对 Numpy 二维矩阵中的行进行分组?

python - 为什么这种情况下 pandas dataframe assign 引发 TypeError

python - Scrapy 一遍又一遍地为德国网站上的不同网址抓取同一页面

python - 使用 Django 的 ORM Extra() 包含重复表

python - Pandas - 查找大小为 n 的行 block ,其中最高值恰好位于中间,而不使用 for 循环

html - 在 HTML 中使用 pandas 数据框

python - Numpy.cumsum 反向

python - 使用 Python 在图像验证码中删除行