javascript - pandas df into nested json -


quite similar question asked there, , brilliantly answered user1609452 in r. still, specific problematic. i'd expand question. let's take same table (mydata):

id  location  l_size   l_color    station    s_size   s_color     category   c_size   c_color   1     alpha     6      #000000      zeta       3      #333333      big       0.63     #306100 2     alpha     6      #000000      zeta       3      #333333     medium     0.43     #458b00 3     alpha     6      #000000      zeta       3      #333333     small      0.47     #6aa232 4     alpha     6      #000000      yota       3      #4c4c4c      big       0.85     #306100 5     alpha     6      #000000      yota       3      #4c4c4c     medium     0.19     #458b00 6     alpha     6      #000000      yota       3      #4c4c4c     small      0.89     #6aa232 7      beta     6      #191919      theta      4      #666666      big       0.09     #306100 8      beta     6      #191919      theta      4      #666666     medium     0.33     #458b00 9      beta     6      #191919      theta      4      #666666     small      0.79     #6aa232 10     beta     6      #191919      theta      4      #666666      big       0.89     #306100 11     beta     6      #191919       meta      3      #7f7f7f     medium     0.71     #458b00 12     beta     6      #191919       meta      3      #7f7f7f     small      0.59     #6aa232 

each category has 1 or multiple attributes (here, one: size). i'd like, it's report size each parent/children in json file:

       {  "name":"mydata",  "size":12,  "color":"#ffffff"  "children":[    {      "name":"alpha",      "size":6,      "color":"#000000"      "children":[         {            "name":"zeta",            "size":3,            "color":"#333333"            "children":[               {                  "name":"big",                  "size":0.63,                  "color":"#306100"               }, ... 

etc. couldn't make in r, nor in pandas... idea?

edit: goal link diverse information children, not size. added color column each main column. initial dataframe big , has lot of information, can't paste here, clarity sake.

second edit: chrisb answer worked! great update. still json file isn't uploaded javascript file. file seems upside down (mydata @ end), , information parent before , after children information:

{      "children":[         {            "color":"#000000",          "children":[               {                  "color":"#4c4c4c",                "children":{                     "color":"#306100",                   "name":"big",                   "size":0.85                },                "name":"yota",                "size":3             },             {                  "color":"#333333",                "children":{                     "color":"#306100",                   "name":"big",                   "size":0.63                },                "name":"zeta",                "size":3             }          ],          "name":"alpha",          "size":6       },       {            "color":"#191919",          "children":[               {                  "color":"#7f7f7f",                "children":{                     "color":"#458b00",                   "name":"medium",                   "size":0.71                },                "name":"meta",                "size":3             },             {                  "color":"#666666",                "children":{                     "color":"#306100",                   "name":"big",                   "size":0.09                },                "name":"theta",                "size":4             }          ],          "name":"beta",          "size":6       }    ],    "name":"mydata",    "size":12 

last edit: works fine. chris removed last part of script wrote when updated it, here is. chris!

data = {'name': 'mydata',         'size': len(mydata),         'children': make_children(mydata, levels)}  print json.dumps(data) 

first, need kind of mapping of makes each level. i'm using tuples of column defines "name" , prefix of other attributes want level, this.

levels = [('location', 'l_'),           ('station', 's_'),           ('category', 'c_')] 

then, it's similar recursive function, columns being picked @ each step (finding columns start prefix) , being added tree zipping the columns / values. there's room clean up, should @ least give idea.

def make_children(df, levels):     if len(levels) == 1:         name, prefix = levels[0]         level_cols = [name] + [c c in df if c.startswith(prefix)]         df = df[level_cols]         key_names = ['name'] + [c.strip(prefix) c in level_cols[1:]]         return dict(zip(key_names, df.values[0]))     else:         h, tail = levels[0], levels[1:]         name, prefix = h         level_cols = [name] + [c c in df if c.startswith(prefix)]          data = []         keys, df_gb in df.groupby(level_cols):             key_names = ['name'] + [c.strip(prefix) c in level_cols[1:]]             d = dict(zip(key_names, keys))             d['children'] = make_children(df_gb, tail)             data.append(d)         return data     

Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -