从此数据帧:
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”应具有“提示”:真,然后具有“提示”:“年龄”,因为“退出”:在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"
}
]
}
]
}
为了实现所需的功能,需要创建两个字典来收集数据并根据条件重新定位输入元素。顺便说一下,您在嵌套字典中使用了不错的技巧。代码中的描述。
= ^ .. ^ =
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
}
]
}
]
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句