简体   繁体   中英

Converting dataframe into nested json

import pandas as pd
import numpy as np
arrays = [
    ["bar", "bar", "baz", "baz", "foo", "foo", "qux", "qux"],
    ["one", "two", "one", "two", "one", "two", "one", "two"],
]
tuples = list(zip(*arrays))
index = pd.MultiIndex.from_tuples(tuples, names=["first", "second"])
s = pd.DataFrame(np.random.randn(8,2), index=index)

I have this dataframe that is multiindex and I want to turn it into a json formatted like:

{
  bar:{
        one:{
             [-0.682927 1.698052]
            }
...
...
...
      }

}

I have tried doing

result = s.to_json(orient='index')


'{"["bar","one"]":{"0":-0.6829271378,"1":1.6980523944},"["bar","two"]":{"0":-1.9606477584,"1":-1.1012649579},"["baz","one"]":{"0":0.3183471185,"1":2.7179798601},"["baz","two"]":{"0":-0.5861751156,"1":-0.6979704724},"["foo","one"]":{"0":0.3188920615,"1":-0.0227116609},"["foo","two"]":{"0":-0.6144677884,"1":-1.4383361965},"["qux","one"]":{"0":1.9803476226,"1":1.169466069},"["qux","two"]":{"0":-1.6677933159,"1":-0.092154096}}'

but it turns into a json that is formated like I don't know how to make it a nested json.

Try with:

s.agg(list,axis=1).unstack('first').to_json()

or:

s.agg(list,axis=1).unstack().to_json(orient='index')

Output:

{
  "bar":{
    "one":[
      -1.0231002147,
      0.4748832006
    ],
    "two":[
      0.541095365,
      -2.3872048015
    ]
  },
  "baz":{
    "one":[
      -1.4714562878,
      -0.6560521361
    ],
    "two":[
      -0.3157270757,
      0.0364883486
    ]
  },
  "foo":{
    "one":[
      -0.9160046224,
      0.1506959462
    ],
    "two":[
      -0.3462537931,
      -0.4722427983
    ]
  },
  "qux":{
    "one":[
      -1.2539589641,
      1.5472394925
    ],
    "two":[
      -0.646765898,
      -2.1194728724
    ]
  }
}

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