Python 层中的数据帧到json

Python 层中的数据帧到json,python,json,pandas,Python,Json,Pandas,我有一个数据帧: school_id city id name batch roll_no age xx ax 1 abc a1 100 13 xx ax 1 dsf a2 200 45 xx ax 2 fas a1 400 23 我必须将其转换为多层json格式: 这样,它会在顶部给出一条消息作为键。 s

我有一个数据帧:

school_id  city  id    name   batch   roll_no  age 
   xx       ax    1    abc     a1      100     13
   xx       ax    1    dsf     a2      200     45
   xx       ax    2    fas     a1      400     23
我必须将其转换为多层json格式: 这样,它会在顶部给出一条消息作为键。 school_id和city在所有行中都是相同的,因此它们处于顶级,并且 然后按id嵌套。 我的结果应该是这样的,如果嵌套在id列中,效果会更好

{
  "message": "The school Table.",
  "school_id": "xx",
  "city": "ax",
  "ids": [
    {
     "id" : 1,
     "name" : "abc",
     "batch" : "a1",
     "roll_no" : 100,
     "age" : 13
     },
     {
     "id" : 1,
     "name" : "abc",
     "batch" : "a1",
     "roll_no" : 100,
     "age" : 13
      },
    {
     "id" : 1,
     "name" : "dsf",
     "batch" : "a2",
     "roll_no" : 200,
     "age" : 45
      },
    {
     "id" : 2,
     "name" : "fas",
     "batch" : "a1",
     "roll_no" : 400,
     "age" : 23
      }
       ]
}

首先添加新列
message
,然后使用自定义函数为
dict
by添加,最后通过以下方式转换为
json


基本上,您希望使用groupby:

import json

outputs = []
ids = ["school_id","city"]

for ind,df in df.groupby(ids):
    output = dict(zip(ids,ind),
                  message="School Table",
                  ids=df.drop(ids,axis=1).to_dict('r'))
    outputs.append(output)

print(json.dumps(outputs[0],indent=2))
产出:

{
  "city": "ax",
  "message": "School Table",
  "ids": [
    {
      "roll_no": 100,
      "id": 1,
      "age": 13,
      "batch": "a1",
      "name": "abc"
    },
    {
      "roll_no": 200,
      "id": 1,
      "age": 45,
      "batch": "a2",
      "name": "dsf"
    },
    {
      "roll_no": 400,
      "id": 2,
      "age": 23,
      "batch": "a1",
      "name": "fas"
    }
  ],
  "school_id": "xx"
}

“消息”:“学校表。”
手动添加到json中?@jezrael是的……该死的,你太快了@安东:他确实是他不断改进解决方案:)。最多只能投1票though@AntonvBR-谢谢。@jezrael我正在尝试您的数据集代码,您以前的解决方案(执行groupby,然后分配新的列d['message']工作正常,但现在您使用df.assign,我无法在结果json中看到“message”
{
  "city": "ax",
  "message": "School Table",
  "ids": [
    {
      "roll_no": 100,
      "id": 1,
      "age": 13,
      "batch": "a1",
      "name": "abc"
    },
    {
      "roll_no": 200,
      "id": 1,
      "age": 45,
      "batch": "a2",
      "name": "dsf"
    },
    {
      "roll_no": 400,
      "id": 2,
      "age": 23,
      "batch": "a1",
      "name": "fas"
    }
  ],
  "school_id": "xx"
}