首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Pandas Dataframe到JSON层次结构

Pandas Dataframe到JSON层次结构
EN

Stack Overflow用户
提问于 2019-07-30 04:28:48
回答 1查看 81关注 0票数 0

我有一个熊猫数据框架,如下所示:

代码语言:javascript
复制
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 (仅第一行的例子):

代码语言:javascript
复制
[
    {
            "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')`输出:

代码语言:javascript
复制
[{"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个不同的二叉树组成

EN

回答 1

Stack Overflow用户

发布于 2019-07-30 07:49:03

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57260961

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档