简体   繁体   English

将带有嵌套字典的 json 转换为带有 python 的数据框

[英]convert json with nested dicts into data frame with python

can someone explain how I convert the following json into a simple data frame with the following headings?有人可以解释我如何将以下 json 转换为具有以下标题的简单数据框吗?

----- sample---- - - - 样本 - -

{
    "last_scanned_block": 14968718,
    "blocks": {
        "13965799": {
            "0x9603846aff5c425277e483de16179a68dbc739debcc5449ea99e45c9d0924430": {
                "165": {
                    "from": "0x0000000000000000000000000000000000000000",
                    "to": "0x01f87c337be5636Cd9B3D48F1159768A7e7837A5",
                    "value": 100000000000000000000000000,
                    "timestamp": "2022-01-08T16:19:02"
                }
            }
        },
        "13965820": {
            "0xd4a4122734a522c40504c8b0ab43b9aa40ac821cd9913179b3ae64e5b166fc57": {
                "226": {
                    "from": "0x01f87c337be5636Cd9B3D48F1159768A7e7837A5",
                    "to": "0xEa3Fa123Eb40CEEaeED390D8d6dE6AF95f044AF7",
                    "value": 610000000000000000000000,
                    "timestamp": "2022-01-08T16:25:12"
                }
            }
        },

--- end---- - - 结尾 - -

I'd like the df to have the following 8 column headings and values for each row我希望 df 具有以下 8 列标题和每行的值

(value examples for first row) (第一行的值示例)

  • Last_scanned_block: 14968718 Last_scanned_block:14968718
  • block: 13965799区块:13965799
  • hex: 0x9603846aff5c425277e483de16179a68dbc739debcc5449ea99e45c9d0924430十六进制:0x9603846aff5c425277e483de16179a68dbc739debcc5449ea99e45c9d0924430
  • number: 165数量:165
  • from: 0x0000000000000000000000000000000000000000来自:0x0000000000000000000000000000000000000000
  • to: 0x01f87c337be5636Cd9B3D48F1159768A7e7837A5至:0x01f87c337be5636Cd9B3D48F1159768A7e7837A5
  • value: 100000000000000000000000000值:100000000000000000000000000
  • timestamp: 2022-01-08T16:19:02时间戳:2022-01-08T16:19:02

Thanks谢谢

I would make a new dictionary from the json that is passed in. Essentially instead of having nested dictionaries like you have above you want to get them into one simple dictionary according to your headings and values.我会根据传入的 json 制作一本新词典。本质上,您不想像上面那样使用嵌套词典,而是希望根据您的标题和值将它们放入一个简单的词典中。 It should be:它应该是:

*heading name* : *list of values*

Essentially, the resulting format should be:本质上,生成的格式应该是:

{"Last_scanned_block" : [14968718], "block" : [13965799], "hex" : ["0x9603846aff5c425277e483de16179a68dbc739debcc5449ea99e45c9d0924430"], "number" : [165], "from" : ["0x0000000000000000000000000000000000000000"], "to" : ["0x01f87c337be5636Cd9B3D48F1159768A7e7837A5"], "value": [100000000000000000000000000], "timestamp" : ["2022-01-08T16:19:02"]}

Then every time you read more data you just append it to each respective list in your dictionary.然后,每次您阅读更多数据时,您只需将 append 放入字典中的每个相应列表即可。

Once you have your complete dictionary, you would use pandas. So something along the lines of:一旦你有了完整的字典,你就会使用 pandas。所以一些事情是这样的:

import pandas
d = *the dictionary above*
frame = pandas.DataFrame(data = d)
print(frame)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM