在for循环中添加if语句

时间:2019-08-14 11:04:59

标签: python pandas dataframe for-loop if-statement

从此数据框中:

tree       cues                        directions   thresholds   exits
 1   PLC2hrOGTT;Age;BMI;TimesPregnant   >;>;>;>   126;29;29.7;6  1;0;1;0.5
 2   PLC2hrOGTT;Age;BMI                 >;>;>     126;29;29.7    0;1;0.5
 3   PLC2hrOGTT;Age;BMI;TimesPregnant   >;>;>;>   126;29;29.7;6  1;0;0;0.5
 4   PLC2hrOGTT;Age;BMI;TimesPregnant   >;>;>;>   126;29;29.7;6  1;1;0;0.5
 5   PLC2hrOGTT;Age;BMI;TimesPregnant   >;>;>;>   126;29;29.7;6  0;1;0;0.5
 6   PLC2hrOGTT;Age;BMI                 >;>;>     126;29;29.7    0;0;0.5 
 7   PLC2hrOGTT;Age;BMI;TimesPregnant   >;>;>;>   126;29;29.7;6  1;1;1;0.5
 8   PLC2hrOGTT;Age;BMI;TimesPregnant   >;>;>;>   126;29;29.7;6  0;0;0;0.5

使用以下代码:

>>> def row_to_tree(row):
...     out = {}
...     pos = [out]
...     for cues, directions, thresholds, exits in zip(*map(lambda x: x.split(";"), row[["cues", "directions", "thresholds", "exits"]].values)):
...             pos = pos[0]
...             pos["cues"] = cues
...             pos["directions"] = directions
...             pos["thresholds"] = thresholds
...             pos["exits"] = exits
...             pos["children"] = [{"cues":True}]
...             pos = pos["children"]
...     pos.append({"cues": False})
...     return out

我可以得到这个期望的输出:

>>> trees = [row_to_tree(row) for i, row in df.iterrows()]
>>> print(json.dumps(trees[0], indent=2))
{
  "cues": "PLC2hrOGTT",
  "directions": ">",
  "thresholds": "126",
  "exits": "1",
  "children": [
    {
      "cues": "Age",
      "directions": ">",
      "thresholds": "29",
      "exits": "0",
      "children": [
        {
          "cues": "BMI",
          "directions": ">",
          "thresholds": "29.7",
          "exits": "1",
          "children": [
            {
              "cues": "TimesPregnant",
              "directions": ">",
              "thresholds": "6",
              "exits": "0.5",
              "children": [
                {
                  "cues": true
                },
                {
                  "cues": false
                }
              ]
            }
          ]
        }
      ]
    }
  ]
}

但是我现在想在此循环中添加一个if语句,所以当我添加子项时我现在想要的是一个if,以便如果exits == 1首先添加True,然后添加子项,如果不是然后先添加子项,然后添加False。 (即,父级“ PLC2hrOGTT”应具有“提示”:True,然后具有“提示”:“ Age”,因为“ PLC2hrOGTT”中的“退出”:为“ 1”)是否可以在此循环中实现?

这是所需的输出:

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

1 个答案:

答案 0 :(得分:0)

要实现所需的功能,需要创建两个字典来收集数据并根据条件重新定位输入元素。顺便说一句,您使用带有嵌套字典的漂亮技巧。代码中的描述。

= ^ .. ^ =

import pandas as pd
from io import StringIO
import json


data = StringIO("""
tree cues directions thresholds exits
1 PLC2hrOGTT;Age;BMI;TimesPregnant >;>;>;> 126;29;29.7;6 1;0;1;0.5
2 PLC2hrOGTT;Age;BMI >;>;> 126;29;29.7 0;1;0.5
3 PLC2hrOGTT;Age;BMI;TimesPregnant >;>;>;> 126;29;29.7;6 1;0;0;0.5
4 PLC2hrOGTT;Age;BMI;TimesPregnant >;>;>;> 126;29;29.7;6 1;1;0;0.5
5 PLC2hrOGTT;Age;BMI;TimesPregnant >;>;>;> 126;29;29.7;6 0;1;0;0.5
6 PLC2hrOGTT;Age;BMI >;>;> 126;29;29.7 0;0;0.5
7 PLC2hrOGTT;Age;BMI;TimesPregnant >;>;>;> 126;29;29.7;6 1;1;1;0.5
8 PLC2hrOGTT;Age;BMI;TimesPregnant >;>;>;> 126;29;29.7;6 0;0;0;0.5
""")

# load data into data frame
df = pd.read_csv(data, sep=' ')


def row_to_tree(row):
    out = {}
    data = [{}, out]
    position = 1
    for cues, directions, thresholds, exits in zip(*map(lambda x: x.split(";"), row[["cues", "directions", "thresholds", "exits"]].values)):
        data = data[position]  # initialise dictionary
        # add elements to dictionary
        data['cues'] = cues
        data['directions'] = directions
        data['tresholds'] = thresholds
        data['exits'] = exits

        # add new empty dictionary at the end
        # handle dictionary starting position selection
        if int(float(exits)) == 1:
            position = 1
            data["children"] = [{"cues":True},{}]
        else:
            position = 0
            data["children"] = [{"cues":True},{"cues":False}]

        # relocate new dictionary to the end
        data = data["children"]

    return out

trees = [row_to_tree(row) for i, row in df.iterrows()]
print(json.dumps(trees[0], indent=2))

输出:

{
  "cues": "PLC2hrOGTT",
  "directions": ">",
  "tresholds": "126",
  "exits": "1",
  "children": [
    {
      "cues": true
    },
    {
      "cues": "Age",
      "directions": ">",
      "tresholds": "29",
      "exits": "0",
      "children": [
        {
          "cues": "BMI",
          "directions": ">",
          "tresholds": "29.7",
          "exits": "1",
          "children": [
            {
              "cues": true
            },
            {
              "cues": "TimesPregnant",
              "directions": ">",
              "tresholds": "6",
              "exits": "0.5",
              "children": [
                {
                  "cues": true
                },
                {
                  "cues": false
                }
              ]
            }
          ]
        },
        {
          "cues": false
        }
      ]
    }
  ]
}