问题描述
我有一个for
循环,该循环遍历用于完成API调用的列表.在每个循环中,我都会通过pandas
和append
到REPL_ID
运行json
响应:
I have a for
loop that is looping over a list for completing an API call. With each loop I have the json
response run through pandas
and append
to REPL_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
的内容.
After it completes, I have it print
the contents of 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'}
}]
我在弄平该列表并为其重新编制索引时遇到了麻烦.该怎么办?
I'm having trouble flattening and reindexing this list. How can this be done?
我只是收集此信息来完成需要NodeId
和ID
的下一个API调用.
I'm only collecting this information for completing the next API call which requires both NodeId
and ID
.
推荐答案
很难直接在代码中实现,但是您可以对输出采取额外的步骤.只需循环遍历您的dic的{{},{},...} dic,获取每个dic,然后将每个条目(键/值)添加到一个新的dic中即可得到一个未嵌套的dic.
It's hard to implement directly in your code but you can just take an extra step with your output. Simply loop over your dic of dics {{},{},...}, take each dic and add each entry (key/values) to a new dic leading to one unnested 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变量添加了重新索引"
added the 'reindexing' via the c variable
这篇关于从Pandas Dataframe展平嵌套的Dict的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!