源代码是一个包含嵌套字典的JSON文件。
我创建了一个顶层defaultdict(dict)和一个for循环来获取第1到7行、column State、Size、Pop的数据帧。
在上面的for循环中,我再次创建了另一个(子) defaultdict(dict) and for循环,以获取第1行到第2行、column City、Size、Pop的数据帧
我将孩子defaultdict(dict)附加到最上面的defaultdict(Dict)
父数据框中的行应对子DataFrame重复
期望输出
State Size Pop City Size Pop
1 MH 120 300 MU 100 150
2 MH 120 300 PU 80 110
3 MH 120 300 NG 75 120
4 MH 120 300 PC 85 110
5 GJ 110 250 SU 70 100
6 GJ 110 250 VA 75 80
7 GJ 110 250 AH 85 120另一个使用输入JSON的示例
输入JSON:
{
"datatop": [
{
"datastate": {
"attributes": {
"Name": "ABC",
"Place": "123"
},
"children": [
{
"datacity": {
"attributes": {
"CName": "EFG",
"CPlace": "12345"
}
}
},
{
"datacity": {
"attributes": {
"CNAME": "HIJ",
"CPlace": "6789"
}
}
}
]
}
},
{
"datastate": {
"attributes": {
"Name": "XYZ",
"Place": "456"
},
"children": [
{
"datacity": {
"attributes": {
"CName": "LMN",
"CPlace": "1123"
}
}
},
{
"datacity": {
"attributes": {
"CName": "OPQ",
"CPlace": "22345"
}
}
}
]
}
}
],
"totalCount": "2"
}预期输出:
Name Place CName CPlace
ABC 123 EFG 12345
ABC 123 HIJ 6798
XYZ 456 LMN 1123
XYZ 456 OPQ 22345发布于 2020-09-12 18:36:47
通过改变我的方法,我能够达到预期的输出。
早些时候,我尝试对子for循环中的数据框中的行重复父数据框中的行(我从父for循环中获得的行)。
新方法:在父for循环之前创建一个空列表。从父for循环到子for循环编排键值对,并创建一个由来自父循环的键值对和来自子(嵌套) for循环的键值对组成的大字典。为了制作唯一的键,我使用了父循环和子循环的索引的字符串连接。子for循环结束后,在其外部(在父for循环的末尾),我将字典转换为data frame并将其附加到列表中。一旦父for循环完成,列表中的数据帧就完整了。最后,我对列表中的数据帧进行了连接。
import json
import pandas as pd
from collections import defaultdict
with open("./input.json") as file:
filedata = json.load(file)
data = filedata['datatop']
dflist = []
for i in range (len(data)):
attribute = (data[i])['datastate']['attributes']
child = (((data[i]['datastate'])['children']))
def_dct_chd = defaultdict(dict)
# For Loop to Iterate over all the subnets of the L3OUT EPG
for j in range (len(child)):
def_dct_chd[str(i)+str(j)]['Name'] = attribute['Name']
def_dct_chd[str(i)+str(j)]['Place'] = attribute['Place']
def_dct_chd[str(i)+str(j)]['CName'] = child[j]['datacity']['attributes']['CName']
def_dct_chd[str(i)+str(j)]['CPlace'] = child[j]['datacity']['attributes']['CPlace']
# Create the Data Frames out of the dictionary and append it to the list
dflist.append(pd.DataFrame(def_dct_chd).T)
# concatenate all the dictionaries inside the list
finaldf = pd.concat(dflist)
finaldf = finaldf.reset_index(drop=True)
print(finaldf)https://stackoverflow.com/questions/63822480
复制相似问题