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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35235778/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 00:38:17  来源:igfitidea点击:

How to merge two JSON file with pandas

pythonjsonpandas

提问by mel

I'm trying to do a python script that merge 2 json files for example:

我正在尝试执行一个合并 2 个 json 文件的 python 脚本,例如:

First file: students.json

第一个文件:students.json

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

Second file: teacher.json

第二个文件:teacher.json

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

So in a first time, to not lose any informations I modify the files to add the status of each person like that:

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

{"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"}}

To do that I did the following code:

为此,我执行了以下代码:

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)

What I want to do is to merge data_student and data_teacher and print the resulting JSON in a json file, but I can only use the standard library, pandas, numpy and scipy.

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

After some tests I realize that some teacher are also students which can be a problem for the merge.

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

采纳答案by Keith

it looks like your JSON files contain "objects" as top-level structures. These map to Python dictionaries. So this should be easy using just Python. Just update the first dictionary with the second.

看起来您的 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)

回答by Régis B.

You should concatenatethe two data frames before converting to JSON:

在转换为 JSON 之前,您应该连接两个数据帧:

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