【发布时间】:2019-12-17 16:42:42
【问题描述】:
我想规范化以下 JSON:
[
{
"studentId": 1,
"studentName": "James",
"schools": [
{
"schoolId": 1,
"classRooms": [
{
"classRoomId": {
"id": 1,
"floor": 2
}
},
{
"classRoomId": {
"id": 3
}
},
],
"teachers": [
{
"teacherId": 1,
"teacherName": "Tom"
},
{
"teacherId": 2,
"teacherName": "Sarah"
}
]
},
{
"schoolId": 2,
"classRooms": [
{
"classRoomId": {
"id": 4
}
}
],
"teachers": [
{
"teacherId": 1,
"teacherName": "Tom"
},
{
"teacherId": 2,
"teacherName": "Sarah"
},
{
"teacherId": 3,
"teacherName": "Tara"
}
]
}
]
}
]
我想在 Python 中得到下表(表格形式):
studentId studentName schoolId classRoomId.id classRoomId.floor teacherId
teacherName
1 James 1 1 2 1 Tom
1 James 1 1 2 2 Sarah
1 James 1 3 1 Tom
1 James 1 3 2 Sarah
1 James 2 4 1 Tom
1 James 2 4 2 Sarah
1 James 2 4 3 Tara
我尝试过像这样使用 Pandas json_normalize 函数:
df1 = json_normalize(test1, ["schools","teachers"], ["studentId", "studentName",["schools","teachers"]])
df2 = json_normalize(test1, ["schools","classRooms"], ["studentId", "studentName",["schools","classRooms"]])
df = pd.concat([df1,df2],axis=1)
但这并没有给我我需要的结构。
它不必在 Pandas 中,Python 中的任何其他库或代码都可以。任何帮助都将不胜感激。谢谢。
【问题讨论】:
-
这个问题有帮助吗? stackoverflow.com/questions/53643406/…