Here is the way from networkx
import networkx as nx
G=nx.from_pandas_edgelist(df, 'CHILD', 'PARENT')
l=list(nx.connected_components(G))
L=[dict.fromkeys(y,x) for x, y in enumerate(l)]
d={k: v for d in L for k, v in d.items()}
df.groupby(df.CHILD.map(d)).agg({'CHILD':'unique','PARENT':'max'})
Out[328]:
PARENT CHILD
CHILD
0 4 [1, 2, 3]
1 12 [10, 11]
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…