I am trying to convert a multi-level hierarchy table into a specific JSON format for a visual I am creating.
I have the data in a pandas dataframe and have tried grouping it by the different levels, but then cannot convert a groupby to a json using pandas. I did also try just converting the dataframe to a json, but the format isn't correct. I am not sure what else to do to get the parent/child format that I am looking for. All the "size" values only need to be 1 so that part seems straightforward enough... Thanks in advance!
**This is what my data looks like**
ColA ColB ColC
Parent1 Child1
Parent1 Child2 Child2A
Parent1 Child2 Child2B
Parent1 Child3 Child2A
Parent2 Child1
Parent2 Child2 Child2A
What I am getting from the pandas dataframe to_json is creating the json column by column, so I am losing the hierarchy aspect of it.
so its:
data = {"Parent1}"{"index #":"col2 value"
What I want is:
data = ({ "name":"TEST",
"children": [
{
"name": "Parent1",
"children":
[
{
"name": "Child1",
"size": "1"
},
{
"name":"Child2",
"children":
[
{
"name":"Child2A",
"size":"1"
},
{
"name":"Child2B",
"size":"1"
},
{
"name":"Child2C",
"size":"1"
},
{
"name":"Child2D",
"size":"1"
},
],
},
{
"name":"Parent2",
"children": [
{
"name":"Child2A",
"size":"1"
},
{
"name":"Child2B",
"size":"1"
},
{
"name":"Child2C",
"size":"1"
},
]
},
]
},
{
"name": "Parent3",
"children":
[
{
"name": "Child1",
"size": "1",
},
{
"name":"Child2",
"children":
[
{
"name":"Child2A",
"size":"1"
},
{
"name":"Child2B",
"size":"1"
},
{
"name":"Child2C",
"size":"1"
},
],
},
{
"name":"Child3",
"children":
[
{
"name":"Child3A",
"size":"1"
},
],
},
],
},
]})