简体   繁体   中英

convert hierarchical data to a specific json format in python

I have a dataframe like below. Each topic has several sub-topics.

pd.DataFrame({'topic': ['A', 'A', 'A', 'B', 'B'],
               'sub-topic': ['A1', 'A2', 'A3', 'B1', 'B3' ],
                'value': [2,12,44,21,1]})

    topic   sub-topic   value
0   A        A1         2
1   A        A2         12
2   A        A3         44
3   B        B1         21
4   B        B3         1

I need to convert it to Json format like below. Within first layer, for example topic A, the value is the sum of all its sub-topics.

{'A': {
    'value': 58,
    'children': {
        'A1': {'value': 2},
        'A2': {'value': 12},
        'A3': {'value': 44}    
       },
    },
 'B': {
     'value': 22,
     'children': {
         'B1': {'value': 21},
         'B3': {'value': 1}
      }
   }
}

Does anyone know how I can convert the data to this specific json? I have no clue how I should approach that. Thanks a lot in advance.

Use cusom function in GroupBy.apply , last use Series.to_dict or Series.to_json :

def f(x):
    d = {'value': x['value'].sum(),
         'children': x.set_index('sub-topic')[['value']].to_dict('index')}
    return (d)

#for dictonary
out = df.groupby('topic').apply(f).to_dict()

#for json
#out = df.groupby('topic').apply(f).to_json()

print (out)

{
    'A': {
        'value': 58,
        'children': {
            'A1': {
                'value': 2
            },
            'A2': {
                'value': 12
            },
            'A3': {
                'value': 44
            }
        }
    },
    'B': {
        'value': 22,
        'children': {
            'B1': {
                'value': 21
            },
            'B3': {
                'value': 1
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM