简体   繁体   English

如何将 json output 转换为 python 中的数据帧

[英]how do you convert json output to a data frame in python

I need to convert this json file to a data frame in python:我需要将此 json 文件转换为 python 中的数据帧:

print(resp2)

    {
  "totalCount": 1,
  "nextPageKey": null,
  "result": [
    {
      "metricId": "builtin:tech.generic.cpu.usage",
      "data": [
        {
          "dimensions": [
            "process_345678"
          ],
          "dimensionMap": {
            "dt.entity.process_group_instance": "process_345678"
          },
          "timestamps": [
            1642021200000,
            1642024800000,
            1642028400000
          ],
          "values": [
            10,
            15,
            12
          ]
        }
      ]
    }
  ]
}

Output needs to be like this: Output 需要是这样的:

metricId    dimensions  timestamps  values
builtin:tech.generic.cpu.usage  process_345678  1642021200000   10
builtin:tech.generic.cpu.usage  process_345678  1642024800000   15
builtin:tech.generic.cpu.usage  process_345678  1642028400000   12

I have tried this:我试过这个:

print(pd.json_normalize(resp2, "data"))

I get invalid syntax, any ideas?我得到无效的语法,有什么想法吗?

Take a look at the examples of json_normalize , and you'll see a list of dictionaries that have the key names of the columns you want, unique to each row.查看json_normalize的示例,您将看到一个字典列表,其中包含您想要的列的键名,每一行都是唯一的。 When you have nested lists/objects, then the columns will be flatten to have dot-notation, but nested arrays will not end up duplicated across rows.当您有嵌套列表/对象时,列将被展平以具有点符号,但嵌套的 arrays 最终不会跨行重复。

Therefore, parse the data into a flat list, then you can use from_records .因此,将数据解析为平面列表,然后您可以使用from_records

data = []
for r in resp2['result']:
    metricId = r['metricId']
    for d in r['data']:
        dimension = d['dimensions'][0]  # unclear why this is an array 
        timestamps = d['timestamps']
        values = d['values']
        for t, v in zip(timestamps, values):
            data.append({'metricId': metricId, 'dimensions': dimension,  'timestamps': t, 'values': v})

df = pd.DataFrame.from_records(data)

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

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