I have a data frame:
Parent Child1 Child2 Child3 Child4 Child5 Child6
0 A A1 B2 -1 -1 -1 -1
1 B B1 -1 -1 -1 -1 -1
2 A1 -1 -1 C1 -1 -1 C2
3 D -1 C2 -1 A1 -1 -1
4 C1 -1 -1 -1 -1 -1 -1
5 C2 -1 -1 -1 -1 -1 -1
6 B1 -1 -1 -1 -1 -1 -1
7 B2 B3 B4 -1 -1 -1 -1
8 B3 -1 -1 -1 -1 -1 -1
9 B4 -1 -1 -1 -1 -1 -1
Source:
df = pd.DataFrame({'Parent': ['A','B','A1','D','C1','C2','B1','B2','B3','B4'],'Child1': ['A1','B1','-1','-1','-1','-1','-1','B3','-1','-1'], 'Child2': ['B2','-1','-1','C2','-1','-1','-1','B4','-1','-1'] , 'Child3' : ['-1','-1','C1','-1','-1','-1','-1','-1','-1','-1'] , 'Child4' : ['-1','-1','-1','A1','-1','-1','-1','-1','-1','-1'],'Child5' : ['-1','-1','-1','-1','-1','-1','-1','-1','-1','-1'] ,'Child6' : ['-1','-1','C2','-1','-1','-1','-1','-1','-1','-1']})
Now, I have an input list with a couple of parents like parent_list = ['A', 'B']. I need to find all the children of all these parents.
So for 'A', there are two children: A1 and B2. A1 again has two children 'C1' and 'C2'. But 'C1' and 'C2' are childless (if all children are '-1' they are childless) and moving on B2 has two children - 'B3' and 'B4'. Both B3 and B4 are childless, and moving on B has only one child: 'B1' and 'B1' is childless.
So the final family list for ['A', 'B'] is going to be ['A', 'B', 'A1', 'B2', 'C1', 'C2', 'B3', 'B4', 'B1']
Here is how far I was able to come:
parent_list= ['A','B']
tmp_list = []
output_list = []
child_list= []
for i in parent_list:
output_list.append(i) if i not in output_list else output_list
parent_list.remove(i)
tmp_list = df.loc[df['Parent'] == i, ['Child1','Child2','Child3','Child4','Child5','Child6']].values.flatten().tolist()
while '-1' in tmp_list: tmp_list.remove('-1')
if tmp_list:
parent_list = parent_list + tmp_list
However, my code only runs for i = 'A' in the parent_list and stops. I Am not sure why it wouldn't iterate any further. when I check parent_list after the frist iteration I do see what I want to see but looping doesnt happen. Where am I doing wrong?
Also if there's any better ways of approaching this problem, please suggest.