0

Given is a DataFrame with pairs of start and end locations in chronological order.

Example:

  1. a-b
  2. g-d
  3. b-g
  4. .....

The goal is to create a "networkx" map to visualize all the routes with more than 2 nodes. Like this: A --> B --> G and so on...

I thought i could probably do this by sub setting the df to where end location == start location.

How would one subset the dataset and loop through it until there is no more end location == start location?

JaDDeL
  • 11
  • 2
  • I think maybe [this post](https://stackoverflow.com/questions/28095646/finding-all-paths-walks-of-given-length-in-a-networkx-graph) would help – Plopp Aug 17 '21 at 15:02

1 Answers1

0

I can propose this code

import networkx as nx
import pandas as pd

df = pd.DataFrame({'start': ['a', 'g', 'b', 'd', 'b'], 'end': ['b', 'd', 'g', 'a', 'e']})

G = nx.Graph()
def add_path(elemnt):
    global G
    G.add_edge(elemnt.start, elemnt.end)

df.apply(add_path, axis=1)
nx.draw(G, with_labels = True)

hope it helps

elouassif
  • 308
  • 1
  • 10