如何转换为层次结构

时间:2019-07-31 15:08:10

标签: python json pandas data-structures

我有此代码:

   tree_definitions = robjects.globalenv['treedefinitions']

    df2 = tree_definitions.drop(['nodes', 'classes'], axis=1)

    tree_data = df2.to_dict(orient='records')
    tree_data = json.dumps(tree_data, indent=2)
    data = {'chart_data': tree_data}
    # return data
    return render_template("index.html", data=data)

返回一个由8个元素组成的数组(所以有8个不同的二叉树):

array with 8 elements(trees)

我想修改数据框,使其返回类似以下内容(层次结构):

{
  "cues": "PLC2hrOGTT",
  "directions": ">",
  "exits": "1",
  "thresholds": "126",
  "children": [
    {
      "cues": "Age",
      "directions": ">",
      "exits": "0",
      "thresholds": "29",
      "children": [
        {
          "cues": "BMI",
          "directions": ">",
          "exits": "1",
          "thresholds": "29.7",
          "children": [
            {
              "cues": "TimesPregnant",
              "directions": ">",
              "exits": "0.5",
              "thresholds": "6",
              "children": [
                {
                  "cues": "True",
                },
                {
                  "cues": "False"
                }
              ]
            },
            {"cues": "True"}
          ]
        },
        {
          "cues": "False",
        }
      ]
    },
    {
      "cues": "True",
    }
  ]
}
在此示例中,

只是数组的第一个元素,以简化它。

P.S。每当exits1时,意味着另一个孩子为'True',反之亦然,0negative

有什么办法可以在python中实现,以便可以在前端将其作为JSON发送?

0 个答案:

没有答案