'using recursive function in python to find end-to-end lineage in a data frame

all. I have this data frame with two columns: source and target.

source target
a c
b c
z v
e a
f a
g b
h g
i g
j i
x k
y z

for target "c", it has two sources: "a" and "b", then for "a", it has two sources: "e" and "f", etc.

I would like to use a recursive function to find all lineages starting from "c". The expected outcome should be:

source target
a c
b c
e a
f a
g b
h g
i g
j i

Any help would be appreciated!

I have tried to use for loop in side a recursive function, however it didn't do the job i expected.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source