将数据帧转换为JSON层次结构

2024-05-08 11:33:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个熊猫数据帧如下:

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"}]

我得到的熊猫数据帧的另一个视图,由8个不同的二叉树组成 This is the dataframe I get, it consists of 8 different binary trees


Tags: jsontruetreeageclassesparentnodesbmi
1条回答
网友
1楼 · 发布于 2024-05-08 11:33:45

您将需要更多地处理数据。您需要将[“提示”、“出口”、“方向”、“阈值”]分成4列。然后,您可以使用groupby来处理(我假设是)“cues0”等等。一旦你按你想要的方式拥有了你的群组,看看这个很棒的代码https://stackoverflow.com/a/50767410/1499803 我不确定这对丢失的值(比如“exits3”和“directions3”列)有什么作用。希望这有帮助。你知道吗

相关问题 更多 >

    热门问题