I have a pandas dataframe as following:
tree nodes classes cues directions thresholds exits
1 1 4 i;i;n;i PLC2hrOGTT;Age;BMI;TimesPregnant >;>;>;> 126;29;29.7;6 1;0;1;0.5
2 2 3 i;i;n PLC2hrOGTT;Age;BMI >;>;> 126;29;29.7 0;1;0.5
3 3 4 i;i;n;i PLC2hrOGTT;Age;BMI;TimesPregnant >;>;>;> 126;29;29.7;6 1;0;0;0.5
4 4 4 i;i;n;i PLC2hrOGTT;Age;BMI;TimesPregnant >;>;>;> 126;29;29.7;6 1;1;0;0.5
5 5 4 i;i;n;i PLC2hrOGTT;Age;BMI;TimesPregnant >;>;>;> 126;29;29.7;6 0;1;0;0.5
6 6 3 i;i;n PLC2hrOGTT;Age;BMI >;>;> 126;29;29.7 0;0;0.5
7 7 4 i;i;n;i PLC2hrOGTT;Age;BMI;TimesPregnant >;>;>;> 126;29;29.7;6 1;1;1;0.5
8 8 4 i;i;n;i PLC2hrOGTT;Age;BMI;TimesPregnant >;>;>;> 126;29;29.7;6 0;0;0;0.5
and I want to convert it to JSON like this (example for the first row only):
[
{
"cues": "PLC2hrOGTT", "directions": ">", "thresholds": "126",
"parent": "null",
"children": [
{
"cues": "Age", "directions": ">", "thresholds": "29",
"parent": "PLC2hrOGTT",
"children": [
{
"cues": "BMI", "directions": ">", "thresholds": "29.7",
"parent": "Age",
"children": [
{
"cues": "TimesPregnant", "directions": ">", "thresholds": "6",
"parent": "BMI",
"children": [
{
"cues": "False",
"parent": "TimesPregnant",
},
{
"cues": "True",
"parent": "TimesPregnant",
}
]
},
{
"cues": "True",
"parent": "BMI",
}
]
},
{
"cues": "False",
"parent": "Age"
},
]
},
{
"cues": "True",
"parent": "PLC2hrOGTT"
},
]
}
];
and so on for each row.
currrently return tree_definitions.to_json(orient='records')
doesn't work. So I wonder is there any way to do this with to_json? or any alternative way, how can I do this?
tree_definitions.to_json(orient='records')` output:
[{"tree":1,"nodes":4,"classes":"i;i;n;i","cues":"PLC2hrOGTT;Age;BMI;TimesPregnant","directions":">;>;>;>","thresholds":"126;29;29.7;6","exits":"1;0;1;0.5"},{"tree":2,"nodes":3,"classes":"i;i;n","cues":"PLC2hrOGTT;Age;BMI","directions":">;>;>","thresholds":"126;29;29.7","exits":"0;1;0.5"},{"tree":3,"nodes":4,"classes":"i;i;n;i","cues":"PLC2hrOGTT;Age;BMI;TimesPregnant","directions":">;>;>;>","thresholds":"126;29;29.7;6","exits":"1;0;0;0.5"},{"tree":4,"nodes":4,"classes":"i;i;n;i","cues":"PLC2hrOGTT;Age;BMI;TimesPregnant","directions":">;>;>;>","thresholds":"126;29;29.7;6","exits":"1;1;0;0.5"},{"tree":5,"nodes":4,"classes":"i;i;n;i","cues":"PLC2hrOGTT;Age;BMI;TimesPregnant","directions":">;>;>;>","thresholds":"126;29;29.7;6","exits":"0;1;0;0.5"},{"tree":6,"nodes":3,"classes":"i;i;n","cues":"PLC2hrOGTT;Age;BMI","directions":">;>;>","thresholds":"126;29;29.7","exits":"0;0;0.5"},{"tree":7,"nodes":4,"classes":"i;i;n;i","cues":"PLC2hrOGTT;Age;BMI;TimesPregnant","directions":">;>;>;>","thresholds":"126;29;29.7;6","exits":"1;1;1;0.5"},{"tree":8,"nodes":4,"classes":"i;i;n;i","cues":"PLC2hrOGTT;Age;BMI;TimesPregnant","directions":">;>;>;>","thresholds":"126;29;29.7;6","exits":"0;0;0;0.5"}]
another view of pandas dataframe I get, consisting of 8 different binary trees