creating df to generate json in the given format

前端 未结 1 1978
情书的邮戳
情书的邮戳 2021-01-14 20:08

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

Json data:

{
 \"name\": \"flare\",
 \"children\":  [
    {
     \"name\": \"K1\",
     \"chi         


        
相关标签:
1条回答
  • 2021-01-14 20:50

    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)
    
    0 讨论(0)
提交回复
热议问题