Home > Back-end >  how to store "networkx info" output in a data frame
how to store "networkx info" output in a data frame

Time:05-22

I want to store output of following NetworkX output into a Pandas data frame:

for i in (node_id):
    G.remove_nodes_from([i])
    (nx.info(G))

Current output looks like follows:

Name: 
Type: Graph
Number of nodes: 262
Number of edges: 455
Average degree:   3.4733
Name: 
Type: Graph
Number of nodes: 261
Number of edges: 425
Average degree:   3.2567

Please, could you tell me a way to store these output into a data frame or dictionary

CodePudding user response:

nx.info outputs a string, you can feed it to pandas.read_csv:

import networkx as nx
import io
import pandas as pd

# dummy graph
G = nx.star_graph(5)

df = pd.read_csv(io.StringIO(nx.info(G)), sep=':\s*', engine='python', names=['attribute', 'value'])
print(df)

Output:

         attribute   value
0             Name     NaN
1             Type   Graph
2  Number of nodes       6
3  Number of edges       5
4   Average degree  1.6667

NB. Note that nx.info is deprecated and will be removed in networkx 3

  • Related