创建df以生成给定格式的json [英] creating df to generate json in the given format

查看:114
本文介绍了创建df以生成给定格式的json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试生成一个df以在json下面生成此代码.

I am trying to generate a df to produce this below json.

Json数据:

{
 "name": "flare",
 "children":  [
    {
     "name": "K1",
     "children": [
      {"name": "Exact", "size": 4},
      {"name": "synonyms", "size": 14}
     ]
    },
    {
     "name": "K2",
     "children": [
      {"name": "Exact", "size": 10},
      {"name": "synonyms", "size": 20}
     ]
    },
     {
     "name": "K3",
     "children": [
      {"name": "Exact", "size": 0},
      {"name": "synonyms", "size": 5}
     ]
    }, 
    {
     "name": "K4",
     "children": [
      {"name": "Exact", "size": 13},
      {"name": "synonyms", "size": 15}
     ]
    },
    {
     "name": "K5",
     "children": [
      {"name": "Exact", "size": 0},
      {"name": "synonyms", "size": 0}
     ]
    }
 ]
}

输入数据:

name    Exact   synonyms
K1        4       14
K2        10      20
K3        0       5
K4        13      15
K5        0       0

我尝试用json中的值创建df,但无法在df.to_json上获取所需的json,请帮忙.

I tried creating df with values in the json but I was not able to get the desired json on df.to_json, please help.

推荐答案

您需要通过 stack ,然后使用

You need reshape data by set_index + stack and then use groupby with apply for nested list of dict:

import json

df = (df.set_index('name')
        .stack()
        .reset_index(level=1)
        .rename(columns={'level_1':'name', 0:'size'})
        .groupby(level=0).apply(lambda x: x.to_dict(orient='records'))
        .reset_index(name='children')
        )

print (df)
  name                                           children
0   K1  [{'name': 'Exact', 'size': 4}, {'name': 'synon...
1   K2  [{'name': 'Exact', 'size': 10}, {'name': 'syno...
2   K3  [{'name': 'Exact', 'size': 0}, {'name': 'synon...
3   K4  [{'name': 'Exact', 'size': 13}, {'name': 'syno...
4   K5  [{'name': 'Exact', 'size': 0}, {'name': 'synon...

#convert output to dict
j = { "name": "flare", "children":  df.to_dict(orient='records')}


#for nice output - easier check
import pprint 
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(j)
{   'children': [   {   'children': [   {'name': 'Exact', 'size': 4},
                                        {'name': 'synonyms', 'size': 14}],
                        'name': 'K1'},
                    {   'children': [   {'name': 'Exact', 'size': 10},
                                        {'name': 'synonyms', 'size': 20}],
                        'name': 'K2'},
                    {   'children': [   {'name': 'Exact', 'size': 0},
                                        {'name': 'synonyms', 'size': 5}],
                        'name': 'K3'},
                    {   'children': [   {'name': 'Exact', 'size': 13},
                                        {'name': 'synonyms', 'size': 15}],
                        'name': 'K4'},
                    {   'children': [   {'name': 'Exact', 'size': 0},
                                        {'name': 'synonyms', 'size': 0}],
                        'name': 'K5'}],
    'name': 'flare'}


#convert data to json and write to file
with open('data.json', 'w') as outfile:
    json.dump(j, outfile)

这篇关于创建df以生成给定格式的json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆