假设这是我的JSON:
ds = [{
"name": "groupa",
"subGroups": [{
"subGroup": 1,
"people": [{
"firstname":"Tony",
},
{
"firstname":"Brian"
}
]
}]
},
{
"name": "groupb",
"subGroups": [{
"subGroup": 1,
"people": [{
"firstname":"Tony",
},
{
"firstname":"Brian"
}
]
}]
}
]
我通过执行以下操作创建了一个Dataframe:
df = json_normalize(ds, record_path =['subGroups', 'people'], meta=['name'])
这给了我:
firstname name
0 Tony groupa
1 Brian groupa
2 Tony groupb
3 Brian groupb
但是,我还想要包含subGroup列。
我试试:
df = json_normalize(ds, record_path =['subGroups', 'people'], meta=['name', 'subGroup'])
但是这给了:
KeyError: 'subGroup'
有什么想法吗?
答案 0 :(得分:1)
json_normalize(
ds,
record_path=['subGroups', 'people'],
meta=[
'name',
['subGroups', 'subGroup'] # each meta field needs its own path
],
errors='ignore'
)
firstname name subGroups.subGroup
0 Tony groupa 1
1 Brian groupa 1
2 Tony groupb 1
3 Brian groupb 1
答案 1 :(得分:1)
试试这个。
df = json_normalize(ds, record_path =['subGroups', 'people'],meta['name'['subGroups', 'subGroup']])