Search code examples
pythonpandaspathchain

Form all possible pathways (list/dict) from dataframe in python


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:

  1. e -> b -> a
  2. f -> c -> a
  3. g -> e -> b -> a

If possible, then representing them with numbers like:

  1. e -> b -> a = 5,2,1
  2. f -> c -> a = 6,3,1
  3. g -> e -> b -> a = 7,5,2,1

Update: From field can contain duplicate entries.


Solution

  • 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']]