如何将两个JSON文件与pandas合并

时间:2016-02-06 00:26:31

标签: python json pandas

我试图做一个合并2个json文件的python脚本,例如:

第一档:students.json

{"John Smith":{"age":16, "id": 1}, ...., "Paul abercom":{"age":18, "id": 764}}

第二档:teacher.json

{"Agathe Magesti":{"age":36, "id": 765}, ...., "Tom Ranliver":{"age":54, "id": 801}}

所以在第一次,为了不丢失任何信息,我修改文件以添加每个人的状态:

{"John Smith":{"age":16, "id": 1, "status":"student"}, ...., "Paul abercom":{"age":18, "id": 764, "status":"student"}}

{"Agathe Magesti":{"age":36, "id": 765, "status":"teacher"}, ...., "Tom Ranliver":{"age":54, "id": 801, "status":"teacher"}}

为此,我做了以下代码:

import pandas as pd
type_student = pd.read_json('student.json')
type_student.loc["status"] = "student"
type_student.to_json("testStudent.json")
type_teacher = pd.read_json('teacher.json')
type_teacher.loc["status"] = "teacher"
type_teacher.to_json("testTeacher.json")
with open("testStudent.json") as data_file:
   data_student = json.load(data_file)
with open("testTeacher.json") as data_file:
   data_teacher = json.load(data_file)

我想要做的是合并data_student和data_teacher并在json文件中打印生成的JSON,但我只能使用标准库,pandas,numpy和scipy。

经过一些测试后,我意识到有些老师也是学生,这可能是合并的问题。

2 个答案:

答案 0 :(得分:1)

在转换为JSON之前,您应该concatenate两个数据框:

pd.concat([data_teacher, data_student], axis=1).to_json()

答案 1 :(得分:1)

看起来您的JSON文件包含“对象”作为顶级结构。这些映射到Python词典。所以使用Python应该很容易。只需用第二个字典更新第一个字典。

import json

with open("mel1.json") as fo:
    data1 = json.load(fo)

with open("mel2.json") as fo:
    data2 = json.load(fo)

data1.update(data2)

with open("melout.json", "w") as fo:
    json.dump(data1, fo)