我有一个熊猫数据帧如下:
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')
不起作用。所以我想知道有没有什么方法可以用tou-json做到这一点?或者其他方法,我怎么做?你知道吗
树_definitions.to\u 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"}]
您将需要更多地处理数据。您需要将[“提示”、“出口”、“方向”、“阈值”]分成4列。然后,您可以使用groupby来处理(我假设是)“cues0”等等。一旦你按你想要的方式拥有了你的群组,看看这个很棒的代码
https://stackoverflow.com/a/50767410/1499803
我不确定这对丢失的值(比如“exits3”和“directions3”列)有什么作用。希望这有帮助。你知道吗相关问题 更多 >
编程相关推荐