我如何将熊猫数据框转换为json以使用d3进行可视化

时间:2019-07-28 15:16:09

标签: json pandas dataframe d3.js

我正在使用flask作为Web服务器,并且在执行return tree_definitions.to_html()时有此数据框:

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,以便可以像这样http://bl.ocks.org/d3noob/8326869这样使用d3进行可视化 但是我对json和d3不太熟悉。

当我return tree_definitions.to_json(orient='columns')时得到:

{"tree":{"1":1,"2":2,"3":3,"4":4,"5":5,"6":6,"7":7,"8":8},"nodes":{"1":4,"2":3,"3":4,"4":4,"5":4,"6":3,"7":4,"8":4},"classes":{"1":"i;i;n;i","2":"i;i;n","3":"i;i;n;i","4":"i;i;n;i","5":"i;i;n;i","6":"i;i;n","7":"i;i;n;i","8":"i;i;n;i"},"cues":{"1":"PLC2hrOGTT;Age;BMI;TimesPregnant","2":"PLC2hrOGTT;Age;BMI","3":"PLC2hrOGTT;Age;BMI;TimesPregnant","4":"PLC2hrOGTT;Age;BMI;TimesPregnant","5":"PLC2hrOGTT;Age;BMI;TimesPregnant","6":"PLC2hrOGTT;Age;BMI","7":"PLC2hrOGTT;Age;BMI;TimesPregnant","8":"PLC2hrOGTT;Age;BMI;TimesPregnant"},"directions":{"1":">;>;>;>","2":">;>;>","3":">;>;>;>","4":">;>;>;>","5":">;>;>;>","6":">;>;>","7":">;>;>;>","8":">;>;>;>"},"thresholds":{"1":"126;29;29.7;6","2":"126;29;29.7","3":"126;29;29.7;6","4":"126;29;29.7;6","5":"126;29;29.7;6","6":"126;29;29.7","7":"126;29;29.7;6","8":"126;29;29.7;6"},"exits":{"1":"1;0;1;0.5","2":"0;1;0.5","3":"1;0;0;0.5","4":"1;1;0;0.5","5":"0;1;0;0.5","6":"0;0;0.5","7":"1;1;1;0.5","8":"0;0;0;0.5"}}

但是我希望它看起来像这样的数据结构示例:

var treeData = [
  {
    "name": "Top Level",
    "parent": "null",
    "children": [
      {
        "name": "Level 2: A",
        "parent": "Top Level",
        "children": [
          {
            "name": "Son of A",
            "parent": "Level 2: A"
          },
          {
            "name": "Daughter of A",
            "parent": "Level 2: A"
          }
        ]
      },
      {
        "name": "Level 2: B",
        "parent": "Top Level"
      }
    ]
  }
];

以便我可以使用d3中的this示例来可视化它

0 个答案:

没有答案