Is there any way to create a dataframe with data from a network?
For example, if I build a graph from an edgelist
G = nx.from_pandas_edgelist(df, 'Col1', 'Col2')
and then I manipulate the network a bit (e.g., removing nodes or calculating all the triangles), and I draw it
G1 = nx.triangles(G)
nx.draw(G1, with_labels=True)
would it be possible to extract the new data that I am plotting? How?
CodePudding user response:
Try G.subgraph(list_of_node_indices).copy()
. In the case of nx.triangles
, it will return a dict with node name as the keys. What you can do is to extract keys with zero value (assuming you want to filter all nodes containing at least one triangle).
Example
G = nx.Graph()
G.add_edge(1,2)
G.add_edge(2,3)
G.add_edge(3,4)
G.add_edge(4,2)
G.add_edge(4,1)
G.add_edge(5,1)
G.add_edge(6,1)
G.add_edge(6,7)
G.add_edge(7,8)
G1 = G.subgraph([ i[0] for i in nx.triangles(G).items() if not i[1]]).copy()
nx.draw(G)
print(nx.triangles(G))
{1: 1, 2: 2, 3: 1, 4: 2, 5: 0, 6: 0, 7: 0, 8: 0}
#only node 6,7,8 is included
print(nx.to_pandas_edgelist(G1))
source target
0 6 7
1 7 8