I am trying to get this dataframe:
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
into something like this (i.e. first row):
{
"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"
}
]
}
]
}
but I am struggling with it and can't seem to make a solution for it, probably as I am not very experienced with python nor pandas.
This is what I have tried to achieve this:
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]
# print(pos)
if exits == 1:
pos.append({"cues": True})
pos["cues"] = cues
pos["directions"] = directions
pos["thresholds"] = thresholds
pos["exits"] = exits
elif exits == 0:
pos["cues"] = cues
pos["directions"] = directions
pos["thresholds"] = thresholds
pos["exits"] = exits
pos.append({"cues": False})
pos["children"] = [{"cues":True}]
pos = pos["children"]
pos.append({"cues": False})
return out
trees = [row_to_tree(row) for i, row in df2.iterrows()]
trees[0]
but it throws me this instead:
{'children': [{'cues': True,
'children': [{'cues': True,
'children': [{'cues': True,
'children': [{'cues': True}, {'cues': False}]}]}]}]}
Which is not what I want. Any help would be highly appreciated.
Aucun commentaire:
Enregistrer un commentaire