从熊猫数据框展平嵌套字典

时间:2018-09-26 16:25:20

标签: json python-3.x pandas

我有一个for循环,该循环遍历用于完成API调用的列表。在每个循环中,我的json响应通过pandasappendREPL_ID

import requests
import json
import pandas as pd
from pandas.io.json import json_normalize

REPL_ID = []

REPL = ["node01%4000-68D6FB-65377D-4804B8-A7F390%5B1-1-2D%5D",
        "node02%4000-B700F9-869677-4991B3-79CBE2%5B1-1-2E%5D",
        "node03%4000-94CF47-90E188-48728F-0538D8%5B1-1-19%5D"
       ]

def get_id():
    for value in REPL:
        url = func_repl(value)
        r = requests.get(url, headers=HEADERS, verify=False)

        jsonstring = json.dumps(r.json()["replication"])
        load = json.loads(jsonstring)
        df = json_normalize(load)
        df['NodeId'] = pd.Series(df.itemNodeId)
        df['ID'] = pd.Series(df.id).str.replace("{", "").str.replace("}", "")
        col = ['NodeId', 'ID']
        df1 = pd.DataFrame(df, columns=col)
        x = df1.to_dict('index')
        REPL_ID.append(x)
    return

完成后,我获得了print的内容REPL_ID

REPL_ID的输出:

[{
  0: {'NodeId': 'node01@00-68D6FB-65377D-4804B8-A7F390[1-1-2D]',
      'ID': '006bdade-49a8-4875-93de-54ba356403c4'},
  ...
  20: {'NodeId': 'node01@00-68D6FB-65377D-4804B8-A7F390[1-1-2D]',
      'ID': 'f8613a7d-30b1-4407-82f0-b92c82c6c422'}
  }, {
  0: {'NodeId': 'node03@00-94CF47-90E188-48728F-0538D8[1-1-19]',
      'ID': '065999f6-a3fe-4b1d-af35-7556efcc530e'},
  ...
  27: {'NodeId': 'node03@00-94CF47-90E188-48728F-0538D8[1-1-19]',
      'ID': 'cf96419c-a188-4b1a-85d6-bde41dbbe3ef'}
}]

所需的输出:

[{
  0: {'NodeId': 'node01@00-68D6FB-65377D-4804B8-A7F390[1-1-2D]',
      'ID': '006bdade-49a8-4875-93de-54ba356403c4'}
  ...
  78: {'NodeId': 'node03@00-94CF47-90E188-48728F-0538D8[1-1-19]',
      'ID': 'cf96419c-a188-4b1a-85d6-bde41dbbe3ef'}
}]

我在弄平该列表并为其重新编制索引时遇到麻烦。该怎么办?

我只是收集此信息来完成需要NodeIdID的下一个API调用。

1 个答案:

答案 0 :(得分:1)

很难直接在代码中实现,但是您可以对输出采取额外的步骤。只需循环遍历您的dic的{{},{},...} dic,获取每个dic,然后将每个条目(键/值)添加到一个新的dic中即可得到一个未嵌套的dic。

original_output = [{
  1: {'NodeId': 'node01@00-68D6FB-65377D-4804B8-A7F390[1-1-2D]',
      'ID': '006bdade-49a8-4875-93de-54ba356403c4'},
  2: {'NodeId': 'node01@00-68D6FB-65377D-4804B8-A7F390[1-1-2D]',
      'ID': 'f8613a7d-30b1-4407-82f0-b92c82c6c422'}
  }, {
  5: {'NodeId': 'node03@00-94CF47-90E188-48728F-0538D8[1-1-19]',
      'ID': '065999f6-a3fe-4b1d-af35-7556efcc530e'},
  6: {'NodeId': 'node03@00-94CF47-90E188-48728F-0538D8[1-1-19]',
      'ID': 'cf96419c-a188-4b1a-85d6-bde41dbbe3ef'}
}]


new_dict = {}
c = 0 
for dic in original_output:
    for key in dic.keys():
        new_dict[c] = dic[key]
        c += 1
new_dict

Out:
{0: {'NodeId': 'node01@00-68D6FB-65377D-4804B8-A7F390[1-1-2D]',
  'ID': '006bdade-49a8-4875-93de-54ba356403c4'},
 1: {'NodeId': 'node01@00-68D6FB-65377D-4804B8-A7F390[1-1-2D]',
  'ID': 'f8613a7d-30b1-4407-82f0-b92c82c6c422'},
 2: {'NodeId': 'node03@00-94CF47-90E188-48728F-0538D8[1-1-19]',
  'ID': '065999f6-a3fe-4b1d-af35-7556efcc530e'},
 3: {'NodeId': 'node03@00-94CF47-90E188-48728F-0538D8[1-1-19]',
  'ID': 'cf96419c-a188-4b1a-85d6-bde41dbbe3ef'}}

编辑:通过c变量添加了“重新索引”