我有一个熊猫数据框架,如下所示:
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我想像这样把它转换成JSON (仅第一行的例子):
[
{
"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"
},
]
}
];对每一行依此类推。
目前,return tree_definitions.to_json(orient='records')不起作用。所以我想知道有什么方法可以用to_json做到这一点吗?或者其他任何方式,我该怎么做呢?
tree_definitions.to_json(orient='records')`输出:
[{"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"}]我得到的熊猫数据帧的另一个视图,由8个不同的二叉树组成

发布于 2019-07-30 07:49:03
您将需要更多地使用您的数据。你需要把“提示”、“退出”、“方向”、“阈值”分成4列。然后你可以使用groupby来处理(我假设是) "cues0“等等。一旦你有了你想要的groupby,看看这个很棒的代码https://stackoverflow.com/a/50767410/1499803我不确定这对缺失值有什么作用(比如在"exits3“和"directions3”列),所以YMMV。希望这能有所帮助。
https://stackoverflow.com/questions/57260961
复制相似问题