我想规范化以下 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" } ] } ] }]我试过像这样使用 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 中的任何其他库或代码都可以。任何帮助都将不胜感激。谢谢你。
1 回答
函数式编程
TA贡献1807条经验 获得超9个赞
因为classRooms并teachers形成了 JSON 的两个不同子树,您将不得不解析它们两次:
classrooms = pd.io.json.json_normalize(json_data, ['schools', 'classRooms'], meta=[
'studentId',
'studentName',
['schools', 'schoolId']
])
teachers = pd.io.json.json_normalize(json_data, ['schools', 'teachers'], meta=[
'studentId',
['schools', 'schoolId']
])
# Merge and rearrange the columns in the order of your sample output
classrooms.merge(teachers, on=['schools.schoolId', 'studentId']) \
[['studentId', 'studentName', 'schools.schoolId', 'classRoomId.id', 'classRoomId.floor', 'teacherId', 'teacherName']]
添加回答
举报
0/150
提交
取消