I am trying to do something that feels forced with networkx but there must be a way. I just don't see the right process in the networkx documentation or google searches. Given
import networkx as nx
G = nx.Graph()
test={'first':['a','b','c'],'next':['b','c','d']}
dftest = pd.DataFrame(data=test)
G = nx.from_pandas_edgelist(dftest,'first','next')
I want to return a 2 column list showing not just the edge relationships, but all connections regardless of nodes in between. That list would look like the below where ',' delimits the list columns and each bullet is a row.
I'm then going to append an inversion of this so a filter of column 1 for a node returns all neighbors regardless of whether they are 'next door' or a neighbor of my neighbor.
Any thoughts on how to do the above with networkx or another network/graph python package
Solving my own question after further research. The below code does it. Welcome suggestions on a more efficient way to do this.
dfNodes = pd.DataFrame()
i=0
for node in G.nodes():
if (i==0):
dfNode = pd.DataFrame(nx.descendants(G,node))
dfNode['first']=node
dfNode.columns.values[0] = "next"
dfNodes =dfNode
else:
dfNode = pd.DataFrame(nx.descendants(G,node))
dfNode['first']=node
dfNode.columns.values[0] = "next"
dfNodes = pd.concat([dfNodes,dfNode])
new_row = {'first': node, 'next': node}
dfNodes.loc[len(dfNodes.index)] = new_row
i=i+1
dfNodes.sort_values(['first','next'])