I am trying to form all possible pathways using a dataframe. I have the following dataframe:
import pandas as pd
data = {'from': ['b','c','d','e','f','g'], 'to': ['a', 'a', 'a', 'b','c','e']}
df = pd.DataFrame.from_dict(data)
print(df)
sample dataframe
Now, I want to make all possible pathways/chain using these 2 columns.Output should look something like this:
If possible, then representing them with numbers like:
Update: From field can contain duplicate entries.
One way from networkx
import networkx as nx
G = nx.from_pandas_edgelist(df, 'from', 'to')
[[*nx.all_simple_paths(G, source=x, target='a')][0] for x in list('efg')]
[['e', 'b', 'a'], ['f', 'c', 'a'], ['g', 'e', 'b', 'a']]